当我运行代码时:
row = db:first_row("SELECT MIN(rowid) from table")
local minID = row.rowid
row = db:first_row("SELECT MAX(rowid) from table")
local maxID = row.rowid
我收到错误说:
Runtime error: ...\db_test\main.lua:33: attempt to call method 'first_row' (a nil value)
stack traceback:
[C]: in function 'first_row'
...\main.lua:33: in main chunk
完全相同的SQL查询在同一数据库上的Python中工作。
“ROWID和INTEGER PRIMARY KEY
每个SQLite表的每一行都有一个64位有符号整数键,用于唯一标识其表中的行。这个整数通常称为“rowid”。可以使用一个与特殊情况无关的名称“rowid”,“oid”或“ rowid ”代替列名来访问rowid值。“
local minId
local maxId
for row in db:nrows("SELECT MIN(rowid) AS `rowmin` FROM " .. table) do
minId = row.rowmin
end
for row in db:nrows("SELECT MAX(rowid) AS `rowmax` FROM " .. table) do
maxId = row.rowmax
end
答案 0 :(得分:1)
如果您使用的是lua-sqlite3包装器,则需要使用db:rows()
方法而不是first_row
进行迭代。电话会是:
row = db:rows("SELECT MIN(rowid) AS `rowmin`, MAX(rowid) AS `rowmax` FROM table")
local minID, maxID = row.rowmin, row.rowmax
基于@CL在评论中的建议:
row = db:rows("SELECT MIN(rowid) AS `rowmin` FROM table")
local minID = row.rowmin
row = db:rows("SELECT MAX(rowid) AS `rowmax` FROM table")
local maxID = row.rowmax