Hello guys, thank you in advance for reading this.
I am learning how to use databases for my game, and I am really stucked with this issue.
I made a file and there is a variable called str, which its value I can store in the content of my database (row 9, column 3)
I created 4 buttons.
2 buttons are for changing the value of the variable str.
the third button is for updating specific content of the database.
and the fourth button is for printing the value of said content. (not the variable)
require "sqlite3" --Open data.db. If the file doesn't exist it will be created local path = system.pathForFile("data.db", system.DocumentsDirectory) db = sqlite3.open( path ) ----MAIN VARIABLE str = "I am str" ----FIRST BUTTON---- local button = display.newImage ('images/boton.png') function button:touch( event ) if (event.phase == "began") then str = "chaos" print ("str = " ..str) end end button:addEventListener("touch", button) ----SECOND BUTTON---- local button2 = display.newImage ('images/boton.png') button2.x = 200 function button2:touch( event ) if (event.phase == "began") then str = "hi" print ("str = " ..str) end end button2:addEventListener("touch", button2) --Handle the applicationExit event to close the db local function onSystemEvent( event ) if( event.type == "applicationExit" ) then db:close() end end local insertMore = false --Setup the table if it doesn't exist local tablesetup = [[CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, content, content2);]] print(tablesetup) db:exec( tablesetup ) local tablesave = [[UPDATE test SET content2=']]..str ..[[' WHERE id = 9]] ----SAVE BUTTON---- updates de db local save = display.newImage('images/boton.png') save.x = 100; save.y = 100 function save:touch (event) if (event.phase == "began") then db:exec( tablesave ) print ("str saved as:" ..str) end end save:addEventListener("touch", save) ----PRINTER BUTTON---- prints the current value of content2 where id == 9. local printer = display.newImage('images/boton.png') printer.x = 180; printer.y = 100 function printer:touch (event) if (event.phase == "began") then for row in db:nrows("SELECT \* FROM test WHERE id = 9") do print (row.content2) end end end printer:addEventListener("touch", printer) for row in db:nrows("SELECT \* FROM test WHERE id = 9") do print (row.content2) end --print the sqlite version to the terminal print( "version " .. sqlite3.version() ) --setup the system listener to catch applicationExit Runtime:addEventListener( "system", onSystemEvent )
My problem is that the database will be updated only with the original value of str, but no the ones I assign from the app by using the buttons.
If I change the value of str directly from the code, then it prints the old value when it first runs, and after I pressed the save button, the value of str changes to the one I put manually into the code.
I would really appreciate that someone with more experience than me could guide me on what I am doing wrong.
Thank you very much and I look forward to your answers.
Best Regards,
Lucas.