· 7 years ago · Sep 12, 2018, 02:50 PM
1-- Using LuaSQL for sqlite3 DB handling.
2
3require('luasql.sqlite3')
4
5local env = assert(luasql.sqlite3()) -- create the context
6local conn = assert(env:connect("test.sqlite")) -- connect to the DB
7
8-- Do some queries.
9assert(conn:execute("CREATE TABLE IF NOT EXISTS tbl1(one varchar(10), two smallint)"))
10assert(conn:execute("INSERT INTO tbl1 VALUES('hello!', 10)"))
11assert(conn:execute("INSERT INTO tbl1 VALUES('goodbye', 20)"))
12
13local cursor = assert(conn:execute("SELECT * FROM tbl1 WHERE one LIKE 'good%'"))
14
15local row = {}
16while cursor:fetch(row) do -- print all elements
17 print(table.concat(row, ' | '))
18end
19
20cursor:close() -- close the record fetching handler
21conn:close() -- close the connection to the DB
22env:close() -- release the context