Skip to content

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.

db_exec(sql, [params])
async
Param Type Description
sql string The SQL statement to execute
params table? Optional array of positional parameters for prepared statements (? placeholders)

params is a positional array. Leading/interior nil holes bind as SQL NULL:

-- b is NULL
db_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 })
Value Type Description
affected number Number of rows affected
local affected = db_exec("INSERT INTO products (id, name, price) VALUES (?, ?, ?)", {
"prod-1", "Widget", 9.99
})
log("inserted " .. affected .. " row(s)")