dreamworkAPI
Ctrl K
GitHub
libraryDocs / sqlite

sqlite

librarysharedmenusource

The local SQLite library wrapper.

Classes & types

Functions

sqlite.castConverts the string values of a result row to the specified types, in-place.
sqlite.escapeConverts a string to a safe string for use in an SQL query.
sqlite.getLastErrorReturns the last error message from the last query.
sqlite.isExistsChecks if a schema object ( table, index, view or trigger ) exists in the database.
sqlite.queryExecutes a SQL query with parameters.
sqlite.queryFirstExecutes a SQL query and returns the first row.
sqlite.queryRowExecutes a SQL query and returns a specific row.
sqlite.queryValueExecutes a SQL query and returns the first value of the first row.
sqlite.rawQueryExecutes a raw SQL query.
sqlite.transactionExecutes a transaction of SQL queries in one block.

#sqlite.cast

sharedmenusource
function sqlite.cast(row: sqlite.QueryRow | nil, column_types: table<string, sqlite.ColumnType>): sqlite.QueryRow | nil

Converts the string values of a result row to the specified types, in-place.

null values are kept as is, except for "blob" columns, since hex( NULL ) is an empty string.

Parameters
NameTypeDescription
rowsqlite.QueryRow | nilThe row to convert.
column_typestable<string, sqlite.ColumnType>The column names mapped to their types.
Returns
TypeNameDescription
sqlite.QueryRow | nilrowThe same row.

#sqlite.escape

sharedmenunodiscardsource
function sqlite.escape(str: string | null | nil): string

Converts a string to a safe string for use in an SQL query.

Parameters
NameTypeDescription
strstring | null | nilThe string to convert.
Returns
TypeNameDescription
stringstrThe safe string.

#sqlite.getLastError

sharedmenunodiscardsource
function sqlite.getLastError(): string | nil

Returns the last error message from the last query.

Returns
TypeNameDescription
string | nilerr_msgThe last error message.

#sqlite.isExists

sharedmenunodiscardsource
function sqlite.isExists(kind_name: string, kind_type?: sqlite.ObjectType): boolean

Checks if a schema object ( table, index, view or trigger ) exists in the database.

Names are compared case-insensitively, like SQLite identifiers.

Only the main database is checked, objects created with CREATE TEMP are not found.

Indexes that SQLite creates implicitly for UNIQUE / PRIMARY KEY constraints are named sqlite_autoindex_<table>_<n> and count as indexes.

Both arguments are escaped, so any string is safe to pass. A nil or empty kind_name never matches, and an unknown kind_type simply returns false.

lua
sqlite.isExists( "dreamwork.storage" )           -- any type
sqlite.isExists( "dreamwork.storage", "table" )  -- tables only
sqlite.isExists( "my_index", "index" )           -- indexes only
Parameters
NameTypeDescription
kind_namestringThe name of the schema object.
kind_typeoptionalsqlite.ObjectTypeThe type of the schema object, any type if nil.
Returns
TypeNameDescription
booleanis_existstrue if a schema object with the given name ( and type ) exists, false otherwise.

#sqlite.query

sharedmenusource
function sqlite.query(query_str: sqlite.Query, ...: sqlite.QueryValue): sqlite.QueryRow[] | nil

Executes a SQL query with parameters.

NULL values in the result are replaced with null.

sql.Query returns SQL NULL as the string "NULL", so a text value that is exactly "NULL" is read as null too, store such data as a blob.

Parameters
NameTypeDescription
query_strsqlite.QueryThe SQL query to execute.
...sqlite.QueryValueThe parameters to use in the query.
Returns
TypeNameDescription
sqlite.QueryRow[] | nilresultThe result of the query.

#sqlite.queryFirst

sharedmenunodiscardsource
function sqlite.queryFirst(str: sqlite.Query, ...: sqlite.QueryValue): sqlite.QueryRow | nil

Executes a SQL query and returns the first row.

Parameters
NameTypeDescription
strsqlite.QueryThe SQL query to execute.
...sqlite.QueryValueThe parameters to use in the query.
Returns
TypeNameDescription
sqlite.QueryRow | nilresultThe first row of the result.

#sqlite.queryRow

sharedmenunodiscardsource
function sqlite.queryRow(str: sqlite.Query, row?: integer, ...: sqlite.QueryValue): sqlite.QueryRow | nil

Executes a SQL query and returns a specific row.

Parameters
NameTypeDescription
strsqlite.QueryThe SQL query to execute.
rowoptionalintegerThe row to return.
...sqlite.QueryValueThe parameters to use in the query.
Returns
TypeNameDescription
sqlite.QueryRow | nilresultThe selected row of the result.

#sqlite.queryValue

sharedmenunodiscardsource
function sqlite.queryValue(str: sqlite.Query, ...: sqlite.QueryValue): sqlite.QueryValue | nil

Executes a SQL query and returns the first value of the first row.

Parameters
NameTypeDescription
strsqlite.QueryThe SQL query to execute.
...sqlite.QueryValueThe parameters to use in the query.
Returns
TypeNameDescription
sqlite.QueryValue | nilvalueThe first value of the first row of the result.

#sqlite.rawQuery

sharedmenusource
function sqlite.rawQuery(query_str: sqlite.Query): sqlite.QueryRow[] | nil

Executes a raw SQL query.

Parameters
NameTypeDescription
query_strsqlite.QueryThe SQL query to execute.
Returns
TypeNameDescription
sqlite.QueryRow[] | nilresultThe result of the query.

#sqlite.transaction

sharedmenusource
function sqlite.transaction(fn: fun( query_fn: ( fun( str: sqlite.Query, ...: sqlite.QueryValue ): ( sqlite.QueryRow[] | nil ) ) ): ( V | nil )): V

Executes a transaction of SQL queries in one block.

Parameters
NameTypeDescription
fnfun( query_fn: ( fun( str: sqlite.Query, ...: sqlite.QueryValue ): ( sqlite.QueryRow[] | nil ) ) ): ( V | nil )The function to execute all SQL queries in one transaction.
Returns
TypeNameDescription
VvalueThe result of function execution.
Generated from the dreamwork meta branch.