Up to now, I was saving some information in a database file (using sqlite3) when my user was adding a new building.to the map. My code was:
game.lua:
addBuildingToDatabase = function (buildingId, x, y, currentPopulation) databaseModuleVar.openDatabase("currentGame.db", system.DocumentsDirectory) local insertQuery = [[INSERT INTO buildings VALUES ( NULL, "]] .. buildingId .. [[","]] .. x .. [[","]] .. y .. [[","]] .. currentPopulation .. [[" );]] globalData.db:exec(insertQuery) return true end
After that, the user could manually save the current progress in a new file:
copyDBto.lua
function M.copyDatabaseTo( filename, destination ) -- If the database is opened, close it if (globalData.db ~= nil and globalData.db:isopen()) then print("DB IS OPEN") globalData.db:close() else print("DB IS NOT OPEN") end assert( type(filename) == "string", "string expected for the first parameter but got " .. type(filename) .. " instead." ) assert( type(destination) == "table", "table expected for the second paramter but bot " .. type(destination) .. " instead." ) local sourceDBpath = system.pathForFile( filename, system.DocumentsDirectory ) local readHandle, errorString = io.open( sourceDBpath, "rb" ) assert( readHandle, "Database at " .. filename .. " could not be read from system.DocumentsDirectory: " ) assert( type(destination.filename) == "string", "filename should be a string, its a " .. type(destination.filename) ) assert( type(destination.baseDir) == "userdata", "baseName should be a valid system directory" ) local destinationDBpath = system.pathForFile( destination.filename, destination.baseDir ) local writeHandle, writeErrorString = io.open( destinationDBpath, "wb" ) assert( writeHandle, "Could not open " .. destination.filename .. " for writing." ) local contents = readHandle:read( "\*a" ) writeHandle:write( contents ) io.close( writeHandle ) io.close( readHandle ) return true end
I have recently changed my game.lua so that the changes are not written into the database directly, only after the user has tapped a Confirm button. The new code is the following:
game.lua:
addBuildingToDatabase = function (buildingId, x, y, currentPopulation) databaseModuleVar.openDatabase("currentGame.db", system.DocumentsDirectory) globalData.query = {} globalData.query = globalData.db:prepare[[INSERT INTO buildings (buildingId, x, y, currentPopulation) VALUES(:p\_buildingId, :p\_x, :p\_y, :p\_currentPopulation)]] globalData.query:bind\_names({p\_buildingId=buildingId, p\_x=x, p\_y=y, p\_currentPopulation=currentPopulation}) return true end
Confirm action:
function M.confirmAction() globalData.query:step() --globalData.db:close() M.resetToolbarsAndButtons() M.stopRunningOtherEvents() end
Unfortunately when doing so, the program crashes and complains about Permissions denied (line 15) when accessing the file to save in copyDBto.lua
It’s not a problem with the folder permissions or the directory because that part worked before. I believe it has something to do with the new code, maybe the file is still open? However I check if the database is open in copyDatabaseTo but it does not enter the loop line 4.
ANy idea what could be wrong?