Can't use the iterator from db:*rows as a variable
|User & Date:||anonymous 2019-09-15 15:30:11|
- Change foundin to "0.9.5-1"
- Change icomment to:
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: <pre> local sqlite3 = require("lsqlite3complete") local db = sqlite3.open_memory() local rows = db:nrows("select 11 as qwe") for row in rows do print(row.qwe) end </pre> This produces the following error message: <pre> 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 ? </pre> 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.<br /> Lua 5.3.5 and 5.1.5 installed with Homebrew (haven't tested with other versions).<br /> MacOSX 10.13.6. For comparison, here's Lua code creating an iterator as a var: <pre> function create_iterator(limit) local i = 0 return function() i = i + 1 if i <= limit then return i end end end iterator = create_iterator(10) for x in iterator do print(x) end </pre>
- Change login to "anonymous"
- Change mimetype to "text/x-fossil-wiki"
- Change private_contact to "ed1c74f3036cfa043d8e02a8b2806e9b50371cf2"
- Change severity to "Minor"
- Change status to "Open"
- Change title to "Can't use the iterator from db:*rows as a variable"
- Change type to "Code_Defect"