I was playing around with ways to get column names from a database table and ran unto an unexpected error when I used the stmt:get_name(n) method. Strangely, the stmt:get_names() method worked as expected. I would really appreciate it if someone could look this over and tell me if I misunderstood something important.
Alias for replication:
Output:
From what I can tell by skimming the lsqlite3.c source code, the stmt object is internally a (pointer to a) C struct. The get_names() method does an explicit count of the number of columns using the sqlite3_column_count(vm) function.
On the other hand, the get_name(n) method assumes the svm->columns member of the struct is accurate and uses that to check the index is in bounds. That seems to be an issue because the columns member is only updated by a limited number of functions - in particular, it's not adjusted by db_prepare.
Did I stumble upon a bug, or am I misunderstanding how SQL works?
Thanks in advance.
Alias for replication:
<aliases>
<alias
match="^ndb_test$"
enabled="y"
group="test"
regexp="y"
send_to="12"
sequence="100"
>
<send>nndb = {}
nndb.namedb = sqlite3.open("nndb.sqlite3")
nndb.loaded = {}
function nndb:init()
self.namedb:execute[[
PRAGMA journal_mode = WAL;
CREATE TABLE IF NOT EXISTS adventurers (
name TEXT PRIMARY KEY,
city TEXT,
enemy INTEGER,
pirate INTEGER,
note TEXT,
date_added DATE
);
]]
end
nndb:init()
function nndb:add(name,city)
if not name or not city then
return
end
self.namedb:execute(string.format(
[[INSERT OR REPLACE INTO adventurers(name, city)
VALUES ('%s', '%s');]], name, city))
end --ndb:add
nndb:add("Rangor", "Eleusis")
--check the table looks right
print("table check:")
for a in nndb.namedb:nrows("SELECT * FROM ADVENTURERS") do
tprint(a)
end
local command = "SELECT * FROM ADVENTURERS"
local statement = nndb.namedb:prepare(command)
print("\\nColumn names:")
tprint(statement:get_names())
print("Number of columns:")
print(statement:get_name(0))
</send>
</alias>
</aliases>
Output:
Quote:
table check:
"city"="Eleusis"
"name"="Rangor"
Column names:
1="name"
2="city"
3="enemy"
4="pirate"
5="note"
6="date_added"
Number of columns:
Run-time error
World: Achaea
Immediate execution
[string "Alias: "]:45: index out of range [0..-1]
stack traceback:
[C]: in function 'get_name'
[string "Alias: "]:45: in main chunk
table check:
"city"="Eleusis"
"name"="Rangor"
Column names:
1="name"
2="city"
3="enemy"
4="pirate"
5="note"
6="date_added"
Number of columns:
Run-time error
World: Achaea
Immediate execution
[string "Alias: "]:45: index out of range [0..-1]
stack traceback:
[C]: in function 'get_name'
[string "Alias: "]:45: in main chunk
From what I can tell by skimming the lsqlite3.c source code, the stmt object is internally a (pointer to a) C struct. The get_names() method does an explicit count of the number of columns using the sqlite3_column_count(vm) function.
On the other hand, the get_name(n) method assumes the svm->columns member of the struct is accurate and uses that to check the index is in bounds. That seems to be an issue because the columns member is only updated by a limited number of functions - in particular, it's not adjusted by db_prepare.
Did I stumble upon a bug, or am I misunderstanding how SQL works?
Thanks in advance.