|Title:||Can't use the iterator from db:*rows as a variable|
|Last Modified:||2019-09-15 15:30:11|
|Version Found In:||0.9.5-1|
anonymous added on 2019-09-15 15:30:11:
In Lua, an iterator is a function that returns new values on successive calls, and the function can be passed around as a variable. However, for some reason this doesn't work with the db:*rows functions of lsqlite3:
local sqlite3 = require("lsqlite3complete") local db = sqlite3.open_memory()
This produces the following error message:
lua: s.lua:5: bad argument #1 to 'for iterator' (:sqlite3:vm expected, got nil) stack traceback: [C]: in for iterator 'for iterator' s.lua:6: in main chunk [C]: in ?
The iteration only works if a *rows function is called directly in a `for` statement.
The same happens with db:rows, db:nrows, and db:urows functions and corresponding stmt:*rows functions for prepared statements. It affects both lsqlite3complete and lsqlite3 with a dynamically loaded sqlite3 lib.
My use-case is that I use Fennel, a Lisp language on top of Lua, and I need to pick just the first row from the results. As a functional language, Fennel doesn't have early returns or `break` statements, but could easily call the iterator just once. More generally, iterators could be used with higher-order functions.
Versions are as follows:
LSQLite3 0.9.5-1, installed with Luarocks.
For comparison, here's Lua code creating an iterator as a var:
function create_iterator(limit) local i = 0 return function() i = i + 1 if i <= limit then return i end end end