SQLite
Class Database

java.lang.Object
  extended bySQLite.Database

public class Database
extends java.lang.Object

Main class wrapping an SQLite database.


Field Summary
protected  int error_code
          Internal last error code for exec() methods.
protected  long handle
          Internal handle for the native SQLite API.
 
Constructor Summary
Database()
           
 
Method Summary
 void busy_handler(BusyHandler bh)
          Establish a busy callback method which gets called when an SQLite table is locked.
 void busy_timeout(int ms)
          Set the timeout for waiting for an SQLite table to become unlocked.
 long changes()
          Return the number of changed rows for the last statement.
 void close()
          Close the underlying SQLite database file.
 Vm compile(java.lang.String sql)
          Compile and return SQLite VM for SQL statement.
 Vm compile(java.lang.String sql, java.lang.String[] args)
          Compile and return SQLite VM for SQL statement.
static boolean complete(java.lang.String sql)
          See if an SQL statement is complete.
 void create_aggregate(java.lang.String name, int nargs, Function f)
          Create aggregate function.
 void create_function(java.lang.String name, int nargs, Function f)
          Create regular function.
 java.lang.String dbversion()
          Return SQLite version number as string.
static java.lang.String error_string(int error_code)
          Return error string given SQLite error code.
 void exec(java.lang.String sql, Callback cb)
          Execute an SQL statement and invoke callback methods for each row of the result set.
 void exec(java.lang.String sql, Callback cb, java.lang.String[] args)
          Execute an SQL statement and invoke callback methods for each row of the result set.
protected  void finalize()
          Destructor for object.
 void function_type(java.lang.String name, int type)
          Set function return type.
 TableResult get_table(java.lang.String sql)
          Convenience method to retrieve an entire result set into memory.
 TableResult get_table(java.lang.String sql, java.lang.String[] args)
          Convenience method to retrieve an entire result set into memory.
 void get_table(java.lang.String sql, java.lang.String[] args, TableResult tbl)
          Convenience method to retrieve an entire result set into memory.
private static void internal_init()
          Internal native initializer.
 void interrupt()
          Abort the current SQLite operation.
 boolean is3()
          Check type of open database.
 int last_error()
          Return the code of the last error occured in any of the exec() methods.
 long last_insert_rowid()
          Return the row identifier of the last inserted row.
 void open_aux_file(java.lang.String filename)
          Open SQLite auxiliary database file for temporary tables.
 void open(java.lang.String filename, int mode)
          Open an SQLite database file.
 void progress_handler(int n, ProgressHandler p)
          Establish a progress callback method which gets called after N SQLite VM opcodes.
 void set_authorizer(Authorizer auth)
          Set authorizer function.
 void set_encoding(java.lang.String enc)
          Set character encoding.
 void trace(Trace tr)
          Set trace function.
static java.lang.String version()
          Return SQLite version number as string.
 void vm_compile_args(java.lang.String sql, Vm vm, java.lang.String[] args)
          Internal compile method, SQLite 3.0 only.
 void vm_compile(java.lang.String sql, Vm vm)
          Internal compile method.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

error_code

protected int error_code
Internal last error code for exec() methods.


handle

protected long handle
Internal handle for the native SQLite API.

Constructor Detail

Database

public Database()
Method Detail

busy_handler

public void busy_handler(BusyHandler bh)
Establish a busy callback method which gets called when an SQLite table is locked.

Parameters:
bh - the object implementing the busy callback method

busy_timeout

public void busy_timeout(int ms)
Set the timeout for waiting for an SQLite table to become unlocked.

Parameters:
ms - number of millisecond to wait

changes

public long changes()
Return the number of changed rows for the last statement.


close

public void close()
           throws Exception
Close the underlying SQLite database file.

Throws:
Exception

compile

public Vm compile(java.lang.String sql)
           throws Exception
Compile and return SQLite VM for SQL statement. Only available in SQLite 2.8.0 and above, otherwise a no-op.

Parameters:
sql - SQL statement to be compiled
Returns:
a Vm object
Throws:
Exception

compile

public Vm compile(java.lang.String sql,
                  java.lang.String[] args)
           throws Exception
Compile and return SQLite VM for SQL statement. Only available in SQLite 3.0 and above, otherwise a no-op.

Parameters:
sql - SQL statement to be compiled
args - arguments for the SQL statement, '%q' substitution
Returns:
a Vm object
Throws:
Exception

complete

public static boolean complete(java.lang.String sql)
See if an SQL statement is complete. Returns true if the input string comprises one or more complete SQL statements.

Parameters:
sql - the SQL statement to be checked

create_aggregate

public void create_aggregate(java.lang.String name,
                             int nargs,
                             Function f)
Create aggregate function.

Parameters:
name - the name of the new function
nargs - number of arguments to function
f - interface of function

create_function

public void create_function(java.lang.String name,
                            int nargs,
                            Function f)
Create regular function.

Parameters:
name - the name of the new function
nargs - number of arguments to function
f - interface of function

dbversion

public java.lang.String dbversion()
Return SQLite version number as string. If the database is not open, unknown is returned.


error_string

public static java.lang.String error_string(int error_code)
Return error string given SQLite error code.

Parameters:
error_code - the error code
Returns:
error string

exec

public void exec(java.lang.String sql,
                 Callback cb)
          throws Exception
Execute an SQL statement and invoke callback methods for each row of the result set.

It the method fails, an SQLite.Exception is thrown and an error code is set, which later can be retrieved by the last_error() method.

Parameters:
sql - the SQL statement to be executed
cb - the object implementing the callback methods
Throws:
Exception

exec

public void exec(java.lang.String sql,
                 Callback cb,
                 java.lang.String[] args)
          throws Exception
Execute an SQL statement and invoke callback methods for each row of the result set. Each '%q' or %Q in the statement string is substituted by its corresponding element in the argument vector.

Example:
   String args[] = new String[1];
   args[0] = "tab%";
   db.exec("select * from sqlite_master where type like '%q'",
           null, args);
 
It the method fails, an SQLite.Exception is thrown and an error code is set, which later can be retrieved by the last_error() method.

Parameters:
sql - the SQL statement to be executed
cb - the object implementing the callback methods
args - arguments for the SQL statement, '%q' substitution
Throws:
Exception

finalize

protected void finalize()
Destructor for object.


function_type

public void function_type(java.lang.String name,
                          int type)
Set function return type. Only available in SQLite 2.6.0 and above, otherwise a no-op.

Parameters:
name - the name of the function whose return type is to be set
type - return type code, e.g. SQLite.Constants.SQLITE_NUMERIC

get_table

public TableResult get_table(java.lang.String sql)
                      throws Exception
Convenience method to retrieve an entire result set into memory.

Parameters:
sql - the SQL statement to be executed
Returns:
result set
Throws:
Exception

get_table

public TableResult get_table(java.lang.String sql,
                             java.lang.String[] args)
                      throws Exception
Convenience method to retrieve an entire result set into memory.

Parameters:
sql - the SQL statement to be executed
args - arguments for the SQL statement, '%q' substitution
Returns:
result set
Throws:
Exception

get_table

public void get_table(java.lang.String sql,
                      java.lang.String[] args,
                      TableResult tbl)
               throws Exception
Convenience method to retrieve an entire result set into memory.

Parameters:
sql - the SQL statement to be executed
args - arguments for the SQL statement, '%q' substitution
tbl - TableResult to receive result set
Returns:
result set
Throws:
Exception

internal_init

private static void internal_init()
Internal native initializer.


interrupt

public void interrupt()
Abort the current SQLite operation.


is3

public boolean is3()
Check type of open database.

Returns:
true if SQLite3 database

last_error

public int last_error()
Return the code of the last error occured in any of the exec() methods. The value is valid after an Exception has been reported by one of these methods. See the Constants class for possible values.

Returns:
SQLite error code

last_insert_rowid

public long last_insert_rowid()
Return the row identifier of the last inserted row.


open_aux_file

public void open_aux_file(java.lang.String filename)
                   throws Exception
Open SQLite auxiliary database file for temporary tables.

Parameters:
filename - the name of the auxiliary file or null
Throws:
Exception

open

public void open(java.lang.String filename,
                 int mode)
          throws Exception
Open an SQLite database file.

Parameters:
filename - the name of the database file
mode - open mode, currently ignored
Throws:
Exception

progress_handler

public void progress_handler(int n,
                             ProgressHandler p)
Establish a progress callback method which gets called after N SQLite VM opcodes.

Parameters:
n - number of SQLite VM opcodes until callback is invoked
p - the object implementing the progress callback method

set_authorizer

public void set_authorizer(Authorizer auth)
Set authorizer function. Only available in SQLite 2.7.6 and above, otherwise a no-op.

Parameters:
auth - the authorizer function

set_encoding

public void set_encoding(java.lang.String enc)
                  throws Exception
Set character encoding.

Parameters:
enc - name of encoding
Throws:
Exception

trace

public void trace(Trace tr)
Set trace function. Only available in SQLite 2.7.6 and above, otherwise a no-op.

Parameters:
tr - the trace function

version

public static java.lang.String version()
Return SQLite version number as string. Don't rely on this when both SQLite 2 and 3 are compiled into the native part. Use the class method in this case.


vm_compile_args

public void vm_compile_args(java.lang.String sql,
                            Vm vm,
                            java.lang.String[] args)
                     throws Exception
Internal compile method, SQLite 3.0 only.

Parameters:
sql - SQL statement
args - arguments for the SQL statement, '%q' substitution
vm - Vm object
Throws:
Exception

vm_compile

public void vm_compile(java.lang.String sql,
                       Vm vm)
                throws Exception
Internal compile method.

Parameters:
sql - SQL statement
vm - Vm object
Throws:
Exception


Contact: Christian Werner