Lua SQLite (database) interface
LuaSQLite 3 is a thin wrapper around the public domain SQLite3 database engine.

The lsqlite3 module supports the creation and manipulation of SQLite3 databases. The exported functions are called with prefix sqlite3. However, most sqlite3 functions are called via an object-oriented interface to either database or SQL statement objects.

This documentation does not attempt to describe how SQLite3 itself works, it just describes the Lua binding and the available functions.

For more information about the SQL features supported by SQLite3 and details about the syntax of SQL statements and queries, please see the SQLite3 documentation http://www.sqlite.org/.

Using some of the advanced features (how to use callbacks, for instance) will require some familiarity with the SQLite3 API.

General technique


  • Open the database with either sqlite3.open or sqlite3.open_memory

  • Execute "action" SQL statements with db:exec

  • To query data from the database:


    • Prepare an SQL statement with db:prepare
    • Get column names with stmt:get_names or similar
    • Step through each row with stmt:step
    • Get column values with stmt:get_values or similar
    • After all rows are extracted, finalize the statement with stmt:finalize


  • Close the database with db:close









Numerical error and result codes

The following constants are defined by module sqlite3:

OK: 0 ERROR: 1 INTERNAL: 2 PERM: 3 ABORT: 4
BUSY: 5 LOCKED: 6 NOMEM: 7 READONLY: 8 INTERRUPT: 9
IOERR: 10 CORRUPT: 11 NOTFOUND: 12 FULL: 13 CANTOPEN: 14
PROTOCOL: 15 EMPTY: 16 SCHEMA: 17 TOOBIG: 18 CONSTRAINT: 19
MISMATCH: 20 MISUSE: 21 NOLFS: 22 FORMAT: 24 RANGE: 25
NOTADB: 26 ROW: 100 DONE: 101

For details about their exact meaning please see the SQLite3 documentation http://www.sqlite.org/.


See Also ...

Topics

DOC_database Database (SQLite)
DOC_lua Lua script extensions
DOC_scripting Scripting

Lua functions

LUA_context:aggregate_count context:aggregate_count (Returns the number of calls to the aggregate step function.)
LUA_context:get_aggregate_data context:get_aggregate_data (Returns the user-definable data field for callback functions.)
LUA_context:result context:result (This function sets the result of a callback function to res.)
LUA_context:result_blob context:result_blob (This function sets the result of a callback function to the binary string in blob.)
LUA_context:result_error context:result_error (This function sets the result of a callback function to the error value in err.)
LUA_context:result_int context:result_int (This function sets the result of a callback function to the integer value in number)
LUA_context:result_null context:result_null (This function sets the result of a callback function to nil.)
LUA_context:result_number context:result_number (This function sets the result of a callback function to the value number.)
LUA_context:result_text context:result_text (This function sets the result of a callback function to the string in str.)
LUA_context:set_aggregate_data context:set_aggregate_data (Set the user-definable data field for callback functions to udata.)
LUA_context:user_data context:user_data (Returns the userdata parameter given in the call to install the callback function.)
LUA_db:busy_handler db:busy_handler (Sets or removes a busy handler for a database)
LUA_db:busy_timeout db:busy_timeout (Sets a busy handler)
LUA_db:changes db:changes (Returns number of changes by the most recent SQL statement)
LUA_db:close db:close (Closes a database)
LUA_db:close_vm db:close_vm (Finalizes all statements that have not been explicitly finalized)
LUA_db:create_aggregate db:create_aggregate (Creates an aggregate callback function)
LUA_db:create_collation db:create_collation (Creates a collation callback)
LUA_db:create_function db:create_function (Creates a callback function)
LUA_db:errcode db:errcode (Returns the most recent result code)
LUA_db:errmsg db:errmsg (Returns the most recent error message)
LUA_db:exec db:exec (Executes SQL statements)
LUA_db:interrupt db:interrupt (Interrupts any pending operation)
LUA_db:isopen db:isopen (Tests if the database is open)
LUA_db:last_insert_rowid db:last_insert_rowid (Returns the key of the most recent insert into the database)
LUA_db:nrows db:nrows (Creates an iterator that returns a table of rows from a SELECT - keyed by name)
LUA_db:prepare db:prepare (Compiles an SQL statement)
LUA_db:progress_handler db:progress_handler (Installs a progress callback function)
LUA_db:total_changes db:total_changes (Returns total number of changes)
LUA_db:trace db:trace (Installs debug trace callback)
LUA_db:urows db:urows (Creates an iterator that returns the rows from a SELECT)
LUA_sqlite3.complete sqlite3.complete (Checks for a complete SQL statement)
LUA_sqlite3.open sqlite3.open (Opens or creates a database)
LUA_sqlite3.open_memory sqlite3.open_memory (Opens a database in memory)
LUA_sqlite3.version sqlite3.version (Returns the SQLite version)
LUA_stmt:bind stmt:bind (Binds a value to a parameter)
LUA_stmt:bind_blob stmt:bind_blob (Binds a blob to a parameter)
LUA_stmt:bind_names stmt:bind_names (Binds values in a table to parameters)
LUA_stmt:bind_parameter_count stmt:bind_parameter_count (Returns the largest parameter index)
LUA_stmt:bind_parameter_name stmt:bind_parameter_name (Returns the name of the n'th paremeter)
LUA_stmt:bind_values stmt:bind_values (Binds values to parameters)
LUA_stmt:columns stmt:columns (Returns number of columns in the result set)
LUA_stmt:finalize stmt:finalize (Frees a prepared statement)
LUA_stmt:get_name stmt:get_name (Returns the name of a column in a result set)
LUA_stmt:get_named_types stmt:get_named_types (Returns a table of names and types for the columns in a result set)
LUA_stmt:get_named_values stmt:get_named_values (Returns a table of names and values for the columns in a result set)
LUA_stmt:get_names stmt:get_names (Returns a table of the names of all columns in a result set)
LUA_stmt:get_type stmt:get_type (Returns the type of a column in a result set)
LUA_stmt:get_types stmt:get_types (Returns a table of the types of all columns in a result set)
LUA_stmt:get_unames stmt:get_unames (Returns a list of names of the columns in a result set)
LUA_stmt:get_utypes stmt:get_utypes (Returns a list of types of the columns in a result set)
LUA_stmt:get_uvalues stmt:get_uvalues (Returns a list of values of the columns in a result set)
LUA_stmt:get_value stmt:get_value (Returns the value of a column in a result set)
LUA_stmt:get_values stmt:get_values (Returns a table of the values of all columns in a result set)
LUA_stmt:isopen stmt:isopen (Checks if the statement is open)
LUA_stmt:nrows stmt:nrows (Creates an iterator that returns a table of rows from a SELECT - keyed by name)
LUA_stmt:reset stmt:reset (Resets an SQL statement, so it can be executed again)
LUA_stmt:rows stmt:rows (Creates an iterator that returns a table of rows from a SELECT - numerically keyed)
LUA_stmt:step stmt:step (Executes a previously-prepared SQL statement)
LUA_stmt:urows stmt:urows (Creates an iterator that returns the rows from a SELECT)

(Help topic: general=lua_sqlite3)

DOC_contents Documentation contents page