Corona Database Working on simulator but not on android device, stops on previous scene, using storyboard

I am using database to keep the highscore record. On storyboard scene change i make file to display highscore. I make database first, then table, then retrieve the record. Check if now made score is more than the existing in database. Update the first existing record.

Its working perfectly fine on the simulator, but on device it stucks on the previous scene and never changes the scene to the highscore file.

It was changing scene before database was implemented.

 

 

 --Include sqlite require "sqlite3" local path = system.pathForFile("myDataBaseASDF.db", system.DocumentsDirectory) db = sqlite3.open( path ) local tablesetup = [[CREATE TABLE IF NOT EXISTS highScoreClassic (id INTEGER PRIMARY KEY, Time, Taps);]] print(tablesetup) db:exec( tablesetup ) local dbRows = 0 local dbTaps = 0 local dbTime = "00:00:00" for row in db:nrows("SELECT \* FROM highScoreClassic WHERE id=1;") do --print( "Row " .. row.id, row.Taps, row.Time ) dbRows = dbRows + 1 dbTaps = row.Taps dbTime = row.Time end function isHighscore() if myData.currentScore \> dbTime then return true elseif myData.currentScore == dbTime and myData.taps \> dbTaps then return true elseif myData.currentScore == dbTime and myData.taps \< dbTaps then return false else return false end end function saveToDataBase() print("updating") local q = [[UPDATE highScoreClassic SET Time=']]..myData.currentScore..[[', Taps=]]..myData.taps..[[WHERE id=1;]] db:exec( q ) end function saveToDataBaseFirstTime() print("inserting") local tablefill =[[INSERT INTO highScoreClassic VALUES (NULL, ']]..myData.currentScore..[[',']]..myData.taps..[[');]] db:exec( tablefill ) end if ( dbRows \> 0 ) then if ( isHighscore() ) then print(dbTime, dbTaps) note = display.newText( "Congratulations !!!\nNew High Score\n" .. "Time: " .. myData.currentScore .. "\nTaps: " .. myData.taps, 10 , display.contentHeight/2, "Harrowprint", 42) saveToDataBase() else note = display.newText( "Time: " .. myData.currentScore .. "\nTaps: " .. myData.taps, 10 , display.contentHeight/2, "Harrowprint", 42) end else note = display.newText( "Congratulations !!!\nNew High Score\n" .. "Time: " .. myData.currentScore .. "\nTaps: " .. myData.taps, 10 , display.contentHeight/2, "Harrowprint", 42) saveToDataBaseFirstTime() print("firsTime") end

I have the same problem. Does somebody has a solution for it?

What errors are you seeing in your console log?  We would be shooting in the dark without knowing what the error is and seeing the code related to error.

For instance, the poster above may very likely have a problem with an image name that he’s trying to load in a scene he’s going to that has nothing to do with the database.  Device’s are case sensitive, the simulator is not.  This is the number 1 problem for why “It works in the sim, doesn’t work on the device”.   These problems are easily solved by looking at the console log on the device is question and see what’s going on.   Even if there are no errors, you can put in print statements that show up there to help narrow down the cause. 

If you do not know how to do this, please see the blog post “Basic Debugging” http://www.coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

And report back when you have some information.  Also, it’s very hard to help you when you don’t provide us enough information to work on.   What version of Corona SDK are you using?  Are you building for iOS or Android?  Are you on a Mac or a PC?  

Are you crashing changing storyboard scenes or with some SQL database issue?

Hello Rob,

I built app for Android and when I look logs in the ADB there is written that table ‘highscores’ cannot be found.

I put the database in the DocumentsDirectory and on the simulator everything works fine, but when I run the game on the android device it crashes (scene is not displaying because I load database first, then display objects).

P.S. - I created that database with the Sqlite browser manually.

The p.s. is likely the cause of your problem.  Your simulator system.DocumentsDirectory does not get included in your apps .apk file (or on iOS in the .app bundle).  You have to either have it in your system.ResorucesDirectory and copy it to system.DocumentsDirectory on first run, or you need to create the database if it doesn’t exist.   The OP’s post above has the code to create the database if it doesn’t exist:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local path = system.pathForFile("myDataBaseASDF.db", system.DocumentsDirectory) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db = sqlite3.open( path ) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local tablesetup = [[CREATE TABLE IF NOT EXISTS highScoreClassic (id INTEGER PRIMARY KEY, Time, Taps);]] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(tablesetup) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db:exec( tablesetup )

Will that code create the database, too?

That’s what it does.

Thanks. It works well now.

I have the same problem. Does somebody has a solution for it?

What errors are you seeing in your console log?  We would be shooting in the dark without knowing what the error is and seeing the code related to error.

For instance, the poster above may very likely have a problem with an image name that he’s trying to load in a scene he’s going to that has nothing to do with the database.  Device’s are case sensitive, the simulator is not.  This is the number 1 problem for why “It works in the sim, doesn’t work on the device”.   These problems are easily solved by looking at the console log on the device is question and see what’s going on.   Even if there are no errors, you can put in print statements that show up there to help narrow down the cause. 

If you do not know how to do this, please see the blog post “Basic Debugging” http://www.coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

And report back when you have some information.  Also, it’s very hard to help you when you don’t provide us enough information to work on.   What version of Corona SDK are you using?  Are you building for iOS or Android?  Are you on a Mac or a PC?  

Are you crashing changing storyboard scenes or with some SQL database issue?

Hello Rob,

I built app for Android and when I look logs in the ADB there is written that table ‘highscores’ cannot be found.

I put the database in the DocumentsDirectory and on the simulator everything works fine, but when I run the game on the android device it crashes (scene is not displaying because I load database first, then display objects).

P.S. - I created that database with the Sqlite browser manually.

The p.s. is likely the cause of your problem.  Your simulator system.DocumentsDirectory does not get included in your apps .apk file (or on iOS in the .app bundle).  You have to either have it in your system.ResorucesDirectory and copy it to system.DocumentsDirectory on first run, or you need to create the database if it doesn’t exist.   The OP’s post above has the code to create the database if it doesn’t exist:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local path = system.pathForFile("myDataBaseASDF.db", system.DocumentsDirectory) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db = sqlite3.open( path ) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local tablesetup = [[CREATE TABLE IF NOT EXISTS highScoreClassic (id INTEGER PRIMARY KEY, Time, Taps);]] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(tablesetup) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db:exec( tablesetup )

Will that code create the database, too?

That’s what it does.

Thanks. It works well now.