|
You need to call insert_stmt:step() before get_names()/get_value(). Check more details on the wiki link. Regards Yong From: <lua-l-bounces@lists.lua.org> on behalf of "Leinen, Rick" <RLeinen@leviton.com> Greetings, I am trying out lsqlite3 for the first time and am new to databases. I understand the sqlite3 statements for the most part, but am have trouble understanding how to extract data using lsqlite3. I see how you can print the values in a database table using db:nrows in a For statement, but what if I want just a single entry? I have tried a couple of ways to do this, one that works and one that doesn’t. These come from looking through the information found at http://lua.sqlite.org/index.cgi/doc/tip/doc/lsqlite3.wiki This one works, but doesn’t seem very elegant: local sqlite3 = require("lsqlite3") local db = sqlite3.open("POE.db") function getvalue(udata,cols,values,names) a = values[1] return 0 end db:exec( 'SELECT event_pending FROM devices where url="" getvalue ) print(a) This one doesn’t: local sqlite3 = require("lsqlite3") local db = sqlite3.open("test.db"); db:exec [[ DROP TABLE if exists numbers; CREATE TABLE numbers(num1,num2,str); INSERT INTO numbers VALUES(1,11,"ABC"); INSERT INTO numbers VALUES(2,22,"DEF"); INSERT INTO numbers VALUES(3,33,"UVW"); INSERT INTO numbers VALUES(4,44,"XYZ"); ]] local insert_stmt = db:prepare('SELECT * FROM numbers WHERE num1=1') x = insert_stmt:get_names() print(x[3]) x = insert_stmt:get_value(0) print(x[1]) The above works until it gets to the line “x = insert_stmt:get_value(0)”. Lua returns the following: C:\work>lua test2.lua str lua: test2.lua:17: misuse of function stack traceback: [C]: in method 'get_value' test2.lua:17: in main chunk [C]: in ? Why doesn’t the second example work? Should I be doing this another way? I thought I would be able to pull queries into a Lua table, but I don’t see a way to do that. Thanks, Rick |