I’m trying to prepare several statements which will only be commit if the user taps the OK button. The problem I’m having is that only one row is inserted in the database. It looks like executing several bind_values in a row won’t add them one after another. How can I get around that?
Code samples:
function M.buildingModeSelected(buildingId) -- tap on the map will add building to "list to be built" globalData.valuesToInsert = {} -- Initialize table. To be used in addBuildingToDatabase. Store temporary information of buildings to be added to the database (when in building mode) databaseModuleVar.openDatabase("currentGame.db", system.DocumentsDirectory) -- opens the database connection globalData.query = {} globalData.query = globalData.db:prepare[[INSERT INTO buildings (buildingId, x, y, currentPopulation) VALUES (?, ?, ?, ?)]] -- preparing the query syntax Runtime:addEventListener('tap', addBuildingToDatabase ) end
addBuildingToDatabase = function (buildingId, x, y, currentPopulation) -- Add the building to the list and binds the values to be added local tempArray = {buildingId, x, y, currentPopulation} -- current building to add to already present buildings table.insert( globalData.valuesToInsert, tempArray ) for k,v in pairs(globalData.valuesToInsert) do globalData.query:bind\_values(v[1], v[2], v[3], v[4]) end return true end
function M.confirmAction() -- triggered when user press OK button. Will commit the transaction with "step()" if (globalData.query ~= nil) then globalData.query:step() globalData.query = nil end end