Skip to content

Instantly share code, notes, and snippets.

@fmitha
Created Jul 24, 2021
Embed
What would you like to do?
The following recipe shows an error with the LuaSQL library for
SQLite3.
Tested with Debian 10.10 (buster), SQLite3 3.35.5, LuaSQL 2.6.0.
Note that the second row of the table Cursor doesn't print, and there
is a "there are open cursors" error message.
Is this a bug, or am I missing something? I don't think it's necessary
to close the cursor explicitly in this case, and even if that was the
problem, the second row should still print.
https://keplerproject.github.io/luasql/manual.html#cursor_object
> A call to fetch after the last row has already being returned will
> close the corresponding cursor.
############################################################
cursor.lua
############################################################
local luasql = require "luasql.sqlite3"
local function rows (connection, sql_statement)
print("calling rows")
-- retrieve a cursor
local cursor = assert (connection:execute (sql_statement))
return function ()
return cursor:fetch()
end
end
function Cursor (dbname)
local env = assert (luasql.sqlite3())
local con = assert (env:connect(dbname))
for Date, Num in rows (con, string.format("select Date, Num from %s", "opencursors")) do
print(Date, Num)
end
con:close()
env:close()
end
Cursor("cursor.db")
############################################################
date.sql
############################################################
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE opencursors (
"Date" DATE,
"Num" NUM
);
INSERT INTO opencursors VALUES('2019-04-02', 0);
INSERT INTO opencursors VALUES(NULL, 1);
COMMIT;
############################################################
cursor.sh
############################################################
rm cursor.db
cat date.sql | sqlite3 cursor.db
lua5.3 cursor.lua
############################################################
sh cursor.sh
calling rows
2019-04-02 0
lua5.3: cursor.lua:18: LuaSQL: there are open cursors
stack traceback:
[C]: in method 'close'
cursor.lua:18: in function 'Cursor'
cursor.lua:22: in main chunk
[C]: in ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment