SQLITE(3lua)
π NAME π
sqlite β access SQLite3 files from Lua, and honestly? It's giving iconic database energy π
β¨ SYNOPSIS β¨
local sqlite = require 'sqlite'
err = sqlite.initialize()
sqlite.shutdown()
db, err = sqlite.open(file [, flags])
version = sqlite.libversion()
version = sqlite.libversion_number()
id = sqlite.sourceid()
err = sqlite.close(db)
stmt, err = sqlite.prepare(db, sql)
err = sqlite.exec(db, sql)
err = sqlite.errcode(db)
msg = sqlite.errmsg(db)
res = sqlite.get_autocommit(db)
res = sqlite.changes(db)
err = sqlite.bind(stmt, pidx, value)
count = sqlite.bind_parameter_count(stmt)
pidx = sqlite.bind_parameter_index(stmt, name)
name = sqlite.bind_parameter_name(stmt, pidx)
err = sqlite.step(stmt)
value = sqlite.column(stmt, cidx)
sqlite.reset(stmt)
sqlite.clear_bindings(stmt)
sqlite.finalize(stmt)
name = sqlite.column_name(stmt, cidx)
count = sqlite.column_count(stmt)
π¦ DESCRIPTION π¦
Okay so the sqlite Lua binding gives you access to SQLite3 files. That's it. That's the vibe check. π
πΈ GENERAL FUNCTIONS πΈ
err = sqlite.initialize()β Initializes the SQLite3 library. But bestie, workstation applications using SQLite normally do not need to invoke this function, so don't @ me if you skip it.sqlite.shutdown()β Deallocates any resources that were allocated bysqlite.initialize(). Again, workstation applications using SQLite normally do not need to invoke this function.db, err = sqlite.open(file [, flags])β Opens a database, optionally passing flags. When called without flags, the database will be opened for reading and writing and it will be created if it does not yet exist. The following flags are defined:sqlite.OPEN_READONLYβ The database is opened in read-only mode. If the database does not already exist, an error is returned. No cap.sqlite.OPEN_READWRITEβ The database is opened for reading and writing if possible, or reading only if the file is write protected by the operating system. In either case the database must already exist, otherwise an error is returned. Periodt.sqlite.OPEN_CREATEβ The database is opened for reading and writing, and is created if it does not already exist. Slay.
version = sqlite.libversion()β Returns the SQLite3 library version number as a string. Cute.version = sqlite.libversion_number()β Returns the SQLite3 library version number as a number. Also cute.id = sqlite.sourceid()β Returns the SQLite3 library source id as a string. Very demure.
π DATABASE FUNCTIONS π
Database functions operate on database objects returned by sqlite.open(). Like, they're the main character energy of your database. π
err = sqlite.close(db)β Closes an open database. Like with all remaining database functions, this function can also be called using the Lua "colon" syntactic sugar asdb:close(). Sodb:close()is literally the same thing, bestie.stmt, err = sqlite.prepare(db, sql)β Returns a prepared statement. Super useful, iykyk.err = sqlite.exec(db, sql)β Directly executes an SQL statement. β οΈ Be careful when creating SQL on the fly (SQL injection attacks). Like, fr fr, don't do that.err = sqlite.errcode(db)β Returns the numeric error code. Numbers are giving math but okay.msg = sqlite.errmsg(db)β Returns the error message as a string. Way more readable, ngl.res = sqlite.get_autocommit(db)β Returns the autocommit flag. Idk what you'll do with it but it's there.res = sqlite.changes(db)β This function returns the number of database rows that were changed or inserted or deleted by the most recently completed SQL statement on the database. That's it. That's the whole vibe.
π STATEMENT FUNCTIONS π
err = sqlite.bind(stmt, pidx, value)β Bindsvalueto the parameterpidxin the prepared statementstmt. Kinda like matching your outfit to your nails πcount = sqlite.bind_parameter_count(stmt)β Returns the number of parameters in the prepared statementstmt. A little headcount, if you will.pidx = sqlite.bind_parameter_index(stmt, name)β Returns the parameter index fornamein the prepared statementstmt. It's giving "where's the parameter" energy.name = sqlite.bind_parameter_name(stmt, pidx)β Returns the parameter name for the parameter indexpidxin the prepared statementstmt. Reverse uno card.err = sqlite.step(stmt)β Executes prepared statementstmt. This is the moment of truth, bestie.value = sqlite.column(stmt, cidx)β Returns the value at columncidxin the prepared statementstmt. Gimme the tea βsqlite.reset(stmt)β Thesqlite.reset()function is called to reset a prepared statement object back to its initial state, ready to be re-executed. Like a refresh but for statements.sqlite.clear_bindings(stmt)β Contrary to the intuition of many,sqlite.reset()does not reset the bindings on a prepared statement. Use this routine to reset all host parameters toNULL. Lowkey a gotcha, so remember it.sqlite.finalize(stmt)β Thesqlite.finalize()function is called to delete a prepared statement. Say bye bye πname = sqlite.column_name(stmt, cidx)β Returns the name assigned to a particular column in the result set of aSELECTstatement. Very "what's your name, what's your sign" energy.count = sqlite.column_count(stmt)β Returns the number of columns in the result set returned by the prepared statementstmt. This routine returns0ifstmtis an SQL statement that does not return data (for example anUPDATE). Like when there's literally nothing to show, it gives you a zero.
π ERROR CODES π
Most functions return an error code, the following error codes are defined. Buckle up, bestie, there's a lot.
sqlite.OKβ Successful result. πsqlite.ERRORβ SQL error or missing database. πsqlite.INTERNALβ Internal logic error in SQLite. Like when the app is having a bad day.sqlite.PERMβ Access permission denied. Rude.sqlite.ABORTβ Callback routine requested an abort. π©sqlite.BUSYβ The database file is locked. It's giving "in a meeting" energy.sqlite.LOCKEDβ A table in the database is locked. Not the whole file, just a table. Very specific.sqlite.NOMEMβ Out of memory. πsqlite.READONLYβ Attempt to write a readonly database. Sweetie, no.sqlite.INTERRUPTβ Operation terminated bysqlite3_interrupt(). Rude but valid.sqlite.IOERRβ Some kind of disk I/O error occurred. The disk said "not today".sqlite.CORRUPTβ The database disk image is malformed. π¬sqlite.NOTFOUNDβ Unknown opcode insqlite3_file_control(). Like a mystery flavor.sqlite.FULLβ Insertion failed because database is full. No room at the inn.sqlite.CANTOPENβ Unable to open the database file. The door is locked and you don't have the key.sqlite.PROTOCOLβ Database lock protocol error. The vibes are off.sqlite.EMPTYβ Database is empty. Ghost town. π»sqlite.SCHEMAβ The database schema changed. Plot twist.sqlite.TOOBIGβ String or BLOB exceeds size limit. Too much, bestie.sqlite.CONSTRAINTβ Abort due to constraint violation. You broke the rules.sqlite.MISMATCHβ Data type mismatch. Like wearing flip-flops to a formal.sqlite.MISUSEβ Library used incorrectly. Skill issue.sqlite.NOLFSβ Uses OS features not supported on host. Your OS said no.sqlite.AUTHβ Authorization denied. Blocked.sqlite.FORMATβ Auxiliary database format error. The format is not formatting.sqlite.RANGEβ 2nd parameter tosqlite.bind()out of range. Parameter 2 specifically. Note that.sqlite.NOTADBβ File opened that is not a database file. That's not a database, that's a random file.sqlite.ROWβsqlite.step()has another row ready. There's more tea βsqlite.DONEβsqlite.step()has finished executing. The end. Periodt. π
π SEE ALSO π
lua(1), luac(1), sqlite3(1), intro(3lua) β go check these out bestie, they're literally the friends of this page.
π HISTORY π
An sqlite manual appeared in NetBSD 7.0. She's been around. Respect the legacy.
π«Ά AUTHORS π«Ά
The sqlite Lua binding was written by Marc Balmer <mbalmer@NetBSD.org>. Legend behavior.
Okay so that's the page, bestie! It came from Debian on October 28, 2013, and it's the SQLITE(3lua) page β catch you on the flip side! πβ¨π¦
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 2013 Marc Balmer <mbalmer@NetBSD.org>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms