PDA

View Full Version : sqlite concatenated query



FredP
May 15th, 2010, 03:59 AM
Hi,

I would like to construct my query using concatenation, but I've got this error in the console : attempt to concatenate a nil value



require 'luasqlsqlite3';
local env = luasql.sqlite3();
local con = assert( env:connect("sqlite_db"))

local valeur = "THEO"
cur = assert (con:execute"SELECT * from CONSIGNE WHERE PIECE = '"..valeur.."'")

row = cur:fetch ({}, "a")
while row do
print(row.PIECE, row.TEMP, row.ZN)
-- reusing the table of results
row = cur:fetch (row, "a")
end

-- close everything

cur:close()
con:close()
env:close()


No problems using a simple query :

cur = assert (con:execute"SELECT * from CONSIGNE WHERE PIECE = 'THEO'")

What is the best way to construct a concatenated query ?

Thanks in advance

Fred

blubberhoofd
May 15th, 2010, 10:42 AM
cur = assert (con:execute"SELECT * from CONSIGNE WHERE PIECE = '"..valeur.."'")I had to use something like this to get it to work IIRC:

cur = assert (con:execute(string.format([[SELECT * from CONSIGNE WHERE PIECE = '%s']], valeur)))similar for inserting items


local var1 = "testvar1"
local var2 = "testvar2"

res = assert (con:execute(string.format([[
INSERT INTO CONSIGNE
VALUES ('%s', '%s')]], var1, var2)
))

FredP
May 15th, 2010, 02:29 PM
It works ! :cool: :)

Thanks a lot ;)