1

How do you use parameterized query in LuaSQLite 3?

I've tried understanding the documentations at:

http://luasqlite.luaforge.net/lsqlite3.html#methods for prepared statements

and looked at the example here:

How to quote values for LuaSQL?

but I can't get this to work:

getTable = function( _key)
    local path = system.pathForFile("database.db", system.ResourceDirectory)
    local dc = sqlite3.open( path )

    local stmt = dc:prepare[[ SELECT * FROM Table WHERE Key = :Key ]]
    local sql = stmt:bind({key=_weaponType})
    return dc:nrows(sql)
end

I get the error:

attempt to index 'stmt' (a nil value)

on the line:

local sql = stmt:bind({key=_weaponType})

P.S. I'm currently developing a game in Corona SDK.

Community
  • 1
  • 1
E S
  • 23
  • 5

2 Answers2

5

You attempt to index 'stmt' (a nil value) error means dc:prepare didn't return anything.

I assume it is actually returning nil,errormsg: check what the error is; your statement is probably ill-formed.

RBerteig
  • 41,948
  • 7
  • 88
  • 128
daurnimator
  • 4,091
  • 18
  • 34
  • I didn't realize it was possible to have more 1 return value in LUA! Thank you, this helped me solve my issue. – E S Nov 22 '11 at 19:01
  • 2
    you just made a Lua Uppercase Accident: its just lua (you don't have PYTHON) :) – daurnimator Nov 22 '11 at 21:18
0

When stmt is a null value is because the prepare instruction failed. Ensure that all the fields exist and the query is valid