I'm brand new to Lua.
Using LuaSql could be very cool.
---------------------------------
-- below job does work, very cool:
---------------------------------
require "luasql.sqlite3"
env = assert (luasql.sqlite3())
con = assert (env:connect("try1.db"))
cur = assert (con:execute[[SELECT name, email from people]])
row = cur:fetch ({}, "a")
while row do
print(string.format([[
Name: %s, Email: %s]]
,row.email))
row = cur:fetch (row, "a")
end
cur:close()
con:close()
env:close()
--------------------------------------------------------
-- but below iterator (from luasql manual) does NOT work
--------------------------------------------------------
function rows (connection, sql_statement)
local cursor = assert (connection:execute (sql_statement))
return function ()
return cursor:fetch()
end
end
require "luasql.sqlite3"
env = assert (luasql.sqlite3())
con = assert (env:connect("try1.db"))
for name, email in rows (con, "select name, email from people") do
print (string.format ("%s: %s", name, email))
end
--cursor:close() without this statement error is: there are open cursors
cursor:close() -- with this statement error is: attempt to index global cursor
con:close()
env:close()