Page 1 of 1

Help with SQLite Prepared Statements

Posted: Thu Jan 08, 2026 8:16 pm
by NathanH
Hi,

I recently ventured into this area while trying to optimize some code. I have had success using prepared statements with SELECT queries and For row In stmt:rows(sql$). I haven't been able, however to use them with an INSERT or UPDATE action query. I tried stmt:exec() and get an error and stmt:Step() and the error says "expecting a variable". Any help would be appreciated. Thanks.

NathanH

Re: Help with SQLite Prepared Statements

Posted: Sun Jan 11, 2026 9:15 pm
by lazi
Here is an excerpt from a working script. Maybe it helps.

Code: Select all

stmt=db:prepare([[INSERT INTO billeditems VALUES(NULL, :bill_nr, :date, :name, :brutto, :vat, :amount, :vatvalue)]])

For i,v In Pairs(bill_tetelek)
	stmt:bind_names ( { bill_nr = bill_response.szamlaszam,
					date = bill_response.date,
					name = v.megnevezes,
					brutto = v.bruttoertek,
					vat = v.afakulcs,
					amount = v.mennyiseg,
					vatvalue = v.afaertek } )

					stmt:dostep()
					stmt:reset()
Next
stmt:finalize()             

Re: Help with SQLite Prepared Statements

Posted: Wed Jan 14, 2026 12:21 am
by NathanH
Thanks lazi,

I was using stmt:step() when I should be using stmt:dostep(). That should make all the difference in the world. Thanks very much!!

NathanH