Help with SQLite Prepared Statements

Discuss any general programming issues here
Post Reply
NathanH
Posts: 131
Joined: Sun Jul 05, 2015 1:29 am
Location: Caldwell, Idaho

Help with SQLite Prepared Statements

Post 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
User avatar
lazi
Posts: 649
Joined: Thu Feb 24, 2011 11:08 pm

Re: Help with SQLite Prepared Statements

Post 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()             
NathanH
Posts: 131
Joined: Sun Jul 05, 2015 1:29 am
Location: Caldwell, Idaho

Re: Help with SQLite Prepared Statements

Post 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
Post Reply