db_exec
Executes INSERT, UPDATE, DELETE, or DDL statements on the SQLite database. Returns the number of rows affected. For DDL statements (CREATE TABLE, etc.), the affected row count is 0.
Signature
Section titled “Signature”db_exec(sql, [params])
asyncParameters
Section titled “Parameters”| Param | Type | Description |
|---|---|---|
sql |
string | The SQL statement to execute |
params |
table? | Optional array of positional parameters for prepared statements (? placeholders) |
nil and SQL NULL
Section titled “nil and SQL NULL”params is a positional array. Leading/interior nil holes bind as SQL NULL:
-- b is NULLdb_exec("INSERT INTO t (a, b, c) VALUES (?, ?, ?)", { 1, nil, 3 })Lua tables do not store a trailing nil — { 1, 2, nil } is the same as { 1, 2 }. Do not end the params table with nil. For a trailing SQL NULL, put NULL in the statement text or ensure a non-nil value comes after that slot:
db_exec("INSERT INTO t (a, b, c) VALUES (?, ?, NULL)", { 1, 2 })Returns
Section titled “Returns”| Value | Type | Description |
|---|---|---|
| affected | number | Number of rows affected |
Example
Section titled “Example”local affected = db_exec("INSERT INTO products (id, name, price) VALUES (?, ?, ?)", { "prod-1", "Widget", 9.99})log("inserted " .. affected .. " row(s)")See Also
Section titled “See Also”- db_query - Execute SELECT queries