In one of my scene files I have the following code:
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
This causes the scene to “crash” and main.lua to be reloaded. What’s interesting is that if I take out the SQL UPDATE statement, the scene runs fine.
Is this a bug with director.lua or am I doing something wrong?
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. [import]uid: 23583 topic_id: 12188 reply_id: 44317[/import]
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! [import]uid: 23583 topic_id: 12188 reply_id: 44481[/import]
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. [import]uid: 31262 topic_id: 12188 reply_id: 44492[/import]
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!
Great to know I can just turn off a setting and make my game work again. [import]uid: 42145 topic_id: 12188 reply_id: 48728[/import]
Having some problem to UPDATE a database I felt on your post.
Hoping it’s still active.
Any idea why this code runs fine except it does not change my database :
local function updateBaseWith(lettertochange)
local sql = "SELECT \* FROM database"
for row in db:nrows(sql) do
local str, match = string.gsub(row.blueletters, lettertochange, "", 1)
-- remove a letter from bluletters then add a letter in redletters
if match \> 0 then
row.blueletters = str
row.redletters = row.redletters..lettertochange
db:exec( "UPDATE database SET blueletters=str, redletters=row.redletters WHERE id=row.id" )
-- print() here confirms that row.blueletters and row.redletters have changed
-- but database is not changed ?...?
end
end
end
updateBaseWith("a")
db:exec( "UPDATE database SET blueletters=str, redletters=row.redletters WHERE id=row.id" )
Above is your current update function… but try the below instead, in my version its using your strings instead of just entering the text “str” and “row.redletters” which is how you had it.
local update1 = "UPDATE database SET blueletters = '"..str.."', redletters = '"..row.redletters.."' WHERE id=row.id"
db:exec( update1 )
Thx for the answer.
I tried your code but with no more success. No terminal error, the db just doesn’t change … [import]uid: 9328 topic_id: 12188 reply_id: 60603[/import]
I’m not sure you can run a statement against the database while you are still looping through a results list, especially where you are trying to update the same table you are reading from.
For testing purposes, just do a static UPDATE statement with known values instead of inside a loop. Then, you can make sure your database is being updated.
For this specific scenario. I would run my select, and push all my data to a lua table. Then, I would loop through that table, doing the update statements
[import]uid: 42145 topic_id: 12188 reply_id: 60655[/import]
Super ! Thx jammydodger and thytoeung, I did it
With the following code ( just for guys who would encounter the same questions - caution, may need optimization and real coder touch
[code]
local function updateBaseWith(lettertochange)
local results,k = {},1
– SELECT from database AND PUSH TABLE
local sql = “SELECT id, blueletters, redletters FROM database”
for row in db:nrows(sql) do
local str, match = string.gsub(row.blueletters, lettertochange, “”, 1)
– remove a letter from bluletters then add a letter in redletters
if match > 0 then
results[k] = {id = row.id, blue = str, red = row.redletters…lettertochange}
k = k+1
end
end
– UPDATE database
for i=1,#results do
local update = “UPDATE database SET blueletters=’”…results[i].blue…"’, redletters=’"…results[i].red"’ WHERE id=’"…results[i].id…"’"
db:exec( update )
end
–
results,k = nil,nil --not sure it’s needed
end
updateBaseWith(“a”)
[/code] [import]uid: 9328 topic_id: 12188 reply_id: 60923[/import]