WildFire
WildFire

Reputation: 11

I want to place this variable into a .db database through sqlite 3

I'm writing a lua program and created a variable for the value the user inputted into a text field. I want to place this variable into a .db database through sqlite 3. How do I show a variable in the insert query?

function addToDatabase()
-- Inserting Rows into Database    
        local insertQuery = [[INSERT INTO test VALUES 
        (NULL, ?, 'endOdomReading', ?, 'business', 'Middle Park', 'Logan', '32', '26.56');, (startOdomReading, date)]]

        db:exec(insertQuery)

end

Upvotes: 1

Views: 223

Answers (1)

tonypdmtr
tonypdmtr

Reputation: 3225

I'm not sure which library you use for SQLite3 access (I don't know Corona). Here's an example using lsqlite3 (which I have renamed to sqlite3 -- no leading l):

function AddToDb(a,b) --insert rows into database using prepared statement
  stmt:reset()
  stmt:bind(1,a)
  stmt:bind(2,b)
  assert(stmt:step() == sqlite3.DONE)
end

db = sqlite3.open ':memory:'
db:exec 'create table xxx(value1,value2)'

stmt = db:prepare 'insert into xxx values(?,?)'

AddToDb('hello','world')
AddToDb('bye','now')

for rowid,a,b in db:urows 'select rowid,value1,value2 from xxx' do
  print(rowid,a,b)
end

db:close()

Upvotes: 1

Related Questions