lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

You need to call




before get_names()/get_value(). Check more details on the wiki link.





From: <> on behalf of "Leinen, Rick" <>
Reply-To: Lua mailing list <>
Date: Friday, September 9, 2016 at 8:26 AM
To: "" <>
Subject: lsqlite3




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


This one works, but doesn’t seem very elegant:


local sqlite3 = require("lsqlite3")

local db ="POE.db")


function getvalue(udata,cols,values,names)

  a = values[1]

  return 0


db:exec( 'SELECT event_pending FROM devices where url="" getvalue )




This one doesn’t:


local sqlite3 = require("lsqlite3")

local db ="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()


x = insert_stmt:get_value(0)




The above works until it gets to the line “x = insert_stmt:get_value(0)”.  Lua returns the following:


C:\work>lua test2.lua


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.