db:nrows

Creates an iterator that returns a table of rows from a SELECT - keyed by name

Prototype

db:nrows(sql)

Description

Creates an iterator that returns the successive rows selected by the SQL statement given in string sql. Each call to the iterator returns a table in which the named fields correspond to the columns in the database. Here is an example:

db:exec[=[
CREATE TABLE numbers(num1,num2);
INSERT INTO numbers VALUES(1,11);
INSERT INTO numbers VALUES(2,22);
INSERT INTO numbers VALUES(3,33);
]=]
for a in db:nrows('SELECT * FROM numbers') do table.print(a) end

This script prints:

num2: 11
num1: 1
num2: 22
num1: 2
num2: 33
num1: 3


Note: You should let the iterator finish (that is, get to the end of the selected rows) otherwise the database remains locked. So, do not "break" out of the for loop.

Lua functions

Topics