SQL UPDATE crashes scene

In one of my scene files I have the following code:

1
2
3
4
5
6
7
8
9
10
11
12
function new()
   db:exec( 'update user set highscore=1 where id=1' )
   for row in db:nrows("SELECT * FROM user") do
     print('id: '..row.id)
     print('highscore: '..row.highscore)
   end
 
   initVars()
 
   return localGroup
        
end

I should add that even though the scene "crashes" the table does get updated correctly. So the SQL statements are working but for some reason are causing the app to crash. The console provides no additional info other than displaying the Corona simulator startup text which indicates that the app crashed and restarted.

What happens in initVars() and where is db initialized?

If you comment out initVars() does it work properly?

Commenting out initVars() doesn't fix the problem.

I initialize the db in main.lua with this code:

1
2
3
require("sqlite3")
 
db = sqlite3.open( system.pathForFile("data.db", system.ResourceDirectory) )

FIGURED IT OUT!!!!

I had copied my data.db file to my project directory for deployment and testing on my iPhone. In the Corona Simulator -> Preferences there is an option to "Relaunch Simulator when project is modified?". This setting is set to "Always" be default. Since a SQL UPDATE modifies the data.db file, this caused the simulator to restart itself. Changing this setting to "Never" resolved the problem!

I put my database in the documents folder. This would probably get around your issue. I find the automatic relaunch very handy since you always have to relaunch anyways.

@davidchiu

Thanks for writing up the solution! I just moved from the Windows Sim to Mac Sim, and was trying to figure this exact thing out! I was able to narrow it down to the UPDATE clause, and actually came in here to find if it was a bug! :P

Great to know I can just turn off a setting and make my game work again. :)

views:1331 update:2011/10/12 18:33:00
corona forums © 2003-2011