lua:db:exec
db.exec( statement )
Execute sql statement.
If statement is a query, return value is a table with the query results.
See also db.escape().
Examples:
Check if a table exists:
if #db.exec[[SELECT name FROM sqlite_master WHERE type='table' AND name='grraka_animals']] <1 then -- table doesn't exist, do something here end
Create a table:
db.exec[[CREATE TABLE grraka_animals( animal_name TEXT, location TEXT)]]
Insert values into table:
db.exec[[INSERT INTO grraka_animals( animal_name, location ) VALUES( 'Dog', 'land')]]
Bulk insert using transaction:
local animals= { {animal_name="Aardvark",location="land" }, {animal_name="Albatross",location="air" }, {animal_name="Alligator",location="water" }, {animal_name="Alpaca",location="land" }, {animal_name="Ant",location="land" } } db.exec[[BEGIN]] for _,entry in pairs(animals) do db.exec( ([[INSERT INTO grraka_animals(animal_name, location) VALUES( '%s', '%s')]]):format( db.escape(entry.animal_name), db.escape(entry.location)) end db.exec[[END]]
Run a query and process the results:
local result=db.exec[[SELECT animal_name, location FROM grraka_animals ORDER BY animal_name]] for _,row in result do say(row.animal_name.." lives in "..row.location) end
See sqlite3 general documentation for further reference on statements.
lua/db/exec.txt · Last modified: 2016/02/08 05:37 by vodur