Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Execute multiple sqlite statements with bind_values
Started by J@V0 Dec 30 2018 10:54 AM

- - - - -
1 reply to this topic
sqlite bind_values database

Best Answer J@V0 , 02 January 2019 - 10:27 AM

I actually managed to get around this by adapting confirmAction:

function M.confirmAction()  

  globalData.objectsToBeAddedToTileTemp = {}

  if (globalData.query ~= nil) then
    for k,v in pairs(globalData.valuesToInsert) do
      globalData.query:bind_values(v[1], v[2], v[3], v[4])
      globalData.query:step()
      globalData.query:reset()
    end
      globalData.query = nil
  end
  
end

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

J@V0

[GLOBAL: userInfoPane.html]
J@V0
  • Enthusiast

  • 32 posts
  • Corona SDK

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

 



[TOPIC: post.html]
#2

J@V0

[GLOBAL: userInfoPane.html]
J@V0
  • Enthusiast

  • 32 posts
  • Corona SDK

  Best Answer

I actually managed to get around this by adapting confirmAction:

function M.confirmAction()  

  globalData.objectsToBeAddedToTileTemp = {}

  if (globalData.query ~= nil) then
    for k,v in pairs(globalData.valuesToInsert) do
      globalData.query:bind_values(v[1], v[2], v[3], v[4])
      globalData.query:step()
      globalData.query:reset()
    end
      globalData.query = nil
  end
  
end



[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: sqlite, bind_values, database