lua:db:exec
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
lua:db:exec [2016/02/08 04:50] – s vodur | lua:db:exec [2016/02/08 05:37] (current) – vodur | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | db.exec( statement ) | + | <font 18px/ |
Execute sql statement. | Execute sql statement. | ||
If statement is a query, return value is a table with the query results. | If statement is a query, return value is a table with the query results. | ||
+ | |||
+ | See also [[: | ||
+ | |||
+ | ===== Examples: ===== | ||
Check if a table exists: | Check if a table exists: | ||
Line 9: | Line 13: | ||
<code lua> | <code lua> | ||
if # | if # | ||
- | |||
-- table doesn' | -- table doesn' | ||
+ | end | ||
+ | </ | ||
+ | \\ | ||
+ | Create a table: | ||
+ | |||
+ | <code lua> | ||
+ | db.exec[[CREATE TABLE grraka_animals( | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | |||
+ | Insert values into table: | ||
+ | |||
+ | <code lua> | ||
+ | db.exec[[INSERT INTO grraka_animals( animal_name, | ||
+ | </ | ||
+ | |||
+ | Bulk insert using transaction: | ||
+ | |||
+ | <code lua> | ||
+ | local animals= | ||
+ | { | ||
+ | {animal_name=" | ||
+ | {animal_name=" | ||
+ | {animal_name=" | ||
+ | {animal_name=" | ||
+ | {animal_name=" | ||
+ | } | ||
+ | |||
+ | db.exec[[BEGIN]] | ||
+ | for _,entry in pairs(animals) do | ||
+ | db.exec( ([[INSERT INTO grraka_animals(animal_name, | ||
+ | VALUES( ' | ||
end | end | ||
+ | db.exec[[END]] | ||
</ | </ | ||
+ | |||
+ | Run a query and process the results: | ||
+ | |||
+ | <code lua> | ||
+ | local result=db.exec[[SELECT animal_name, | ||
+ | |||
+ | for _,row in result do | ||
+ | say(row.animal_name.." | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | See sqlite3 general documentation for further reference on statements. | ||
lua/db/exec.1454907015.txt.gz · Last modified: 2016/02/08 04:50 by vodur