Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.
ColonelThirtyTwo edited this page Sep 6, 2012 · 8 revisions

This works like the lsqlite3 library, with a few alterations.

The constant SQLITE_<VAL> can be accessed as lsqlite3.<VAL> (remove the SQLITE_ prefix) Functions throw errors on non-SQLITE_OK return codes unless otherwise specified.

Basic functions:

  • local db, msg, code = lsqlite3.open(filename[, mode]): Opens a database and returns a handle object. mode is optional and can be either "read" for read-only, "write" for read-write, or "create" (the default) for read-write+create if the db doesn't exist. If opening fails, returns nil, an error message, and error code.

  • db:close(): Closes the database.

  • db:exec(sql) or db(sql): Executes a string of SQL via sqlite3_exec

  • db:prepare(sql): Prepares a statement and returns the statement object.

  • stmt:bind(index, value, [length]) or stmt[index] = value: Binds a value to a statement (only supports numerical indexes at the moment). Valid types are number (double), boolean (int true=1, false=0), nil (null), 64-bit signed integers or FFI datatypes (which are casted to void* and need an additional 'length' parameter).

  • stmt:bind_blob(index, value, len): Binds a blob to a statement. Valid types are Lua strings (in which case the len field may be omitted), cdata objects, or nil (which stores a zero blob).

  • stmt:step() or stmt(): Steps the statement. Returns true if the statement has a row available or false if the statement is completed. Ex. while stmt:step() do ... end

  • stmt:get_value(col) or stmt[col]: Returns a value in a column. 64bit-integers are truncated into Lua doubles, and blob values are returned as-is, with no copying done. Strings are converted into Lua strings.

  • stmt:finalize(): Finalizes a statement

  • stmt:reset(): Resets a statement

Blob IO and other functions are also implemented; see the code for a list.

Debug Mode

lsqlite3 keeps track of all opened statements and blobs, and will throw errors when trying to close the database with unfinalized objects. If you set lsqlite3.DEBUG = true, then a traceback will be taken when a statement is prepared or blob is opened, which can be helpful when trying to track them down. This information can be printed via db:dump_unfinalized_statements() and db:dump_unclosed_blobs()

Clone this wiki locally