sqlite#

local sqlite = require 'sqlite'

The sqlite module is a Lua binding for SQLite3.

Functions#

sqlite.open(db)#

Open the sqlite databse given by the string db. This will usually be a path to a sqlite database, but it can be any valid SQLite database string.

Return type:

sqlitedb

Version History

Version

Notes

0.0.1

Added

sqlite.memory_used()#

Return memory the SQLite has allocated in bytes.

Return type:

integer

Version History

Version

Notes

0.0.1

Added

sqlite.memory_used()

Return the maximum amount of memory SQLite has used since the overlay has started.

Return type:

integer

Version History

Version

Notes

0.0.1

Added

Classes#

class sqlite.sqlitedb#

A SQLite database connection.

prepare(sql)#

Prepare the given sql statement. An error will be raised if an error occurs during the prepare, otherwise this method returns a new sqlitestatement.

Return type:

sqlitestatement

Version History

Version

Notes

0.0.1

Added

execute(sql)#

Execute the given sql statement and return the results, if any.

This is a convenience method that calls prepare() then sqlitestatement.step() to get all results and then sqlitestatement.finalize() before returning the results.

Return type:

table

Version History

Version

Notes

0.0.1

Added

class sqlite.sqlitestatement#

A SQLite statement. Statements are prepared SQL statements that can be ran against the database. They can container placeholders that can be set using bind(). See SQLite parameters.

bind(key, value[, blob])#

Set the statement parameter to the given value.

key can be either an integer or a string and much match the parameters specified when this statement was created. See SQLite parameters.

Important

Numbered parameters begin with 1

Parameters:
  • key

  • value

  • blob (boolean) – (Optional) Bind the parameter as a BLOB instead of autodetecting type.

Version History

Version

Notes

0.0.1

Added

0.1.0

Added ``blob`` argument

step()#

Step this statement.

The first time this function is called it will execute this statement. If the statement returns data, the first row will be returned. Remaining rows can be returned by calling this function until it returns nil.

If an error occurs during execution a Lua error will be raised.

Return type:

table

Example#
local db = sqlite.open('foo.db')

local stmt = db:prepare("SELECT * FROM atable WHERE id = ?")
stmt:bind(1, 1234) -- bind 1234 to the first parameter (id)

local results = {}

-- step can be wrapped in an iterator
local function allrows()
    return stmt:step()
end

-- which can be used with for
for row in allrows do
    table.insert(results, row)
end

stmt:finalize()

Version History

Version

Notes

0.0.1

Added

reset()#

Reset the statement so it can executed again.

Important

Resetting a statement does not clear out parameter values. Parameter values will remain set to the last value.

Version History

Version

Notes

0.0.1

Added

finalize()#

Finalize this statement.

Important

This destroys the underlying SQLite statement and will automatically be called by Lua when the Lua variable holding this statement goes out of scope or is otherwise garbage collected.

Module authors may wish to call this manually in certain cases, such as when manually using transactions.

Version History

Version

Notes

0.0.1

Added