SQLite save highscore

I’m using sqlite to save my data. I can’t get my score to save.

Were I have …bestScore…, I’ve tried it without the dots(…) and it still doesn’t work.

How do I do it?

 if bestScore \< score then   
 bestScore = score   
 print("new highscore"..bestScore )  
  
 db:exec[[  
 UPDATE gamestats SET content2=..bestScore.. WHERE content='level1score';  
 ]]  
 end   
  

Thanks,
Dan [import]uid: 78446 topic_id: 14688 reply_id: 314688[/import]

Not sure if there’s a good reason to use square brackets rather than quotes, so try this:

[lua]local sql = “UPDATE gamestats SET content2=” …tostring(bestScore) … " WHERE content=‘level1score’;"
db:exec(sql)[/lua]

You can, of course, combine both lines into one but when something seems wonky with a query you can easily put print(sql) in there and see what you’re passing to the database. Sometimes it’s not what you think. :slight_smile:

Jay
[import]uid: 9440 topic_id: 14688 reply_id: 54321[/import]

the " …tostring(bestScore) … " actually saves …tostring(bestScore) … into the DB

I have a print it’s not sending anything. I have a print for bestScore and it does have a value. If I actually put a number in the

UPDATE gamestats SET content2=50 WHERE content='level1score';  
  

It will save correctly.

Dan [import]uid: 78446 topic_id: 14688 reply_id: 54324[/import]


the " …tostring(bestScore) … " actually saves …tostring(bestScore) … into the DB

Do you still have the square brackets around the query or did you take them out?

Jay [import]uid: 9440 topic_id: 14688 reply_id: 54326[/import]

The brackets are there-

[code]

db:exec[[
UPDATE gamestats SET content2=…bestScore… WHERE content=‘level1score’;
]]

[/code] [import]uid: 78446 topic_id: 14688 reply_id: 54329[/import]

If you’re in love with brackets then try this:

[lua](example code deleted)[/lua]

Not tested, because I don’t love brackets. :slight_smile:

Jay

PS - The first example I wrote is just a better way to do things. Double-quotes instead of brackets, just like every other language on the planet. By continuing to use the brackets the only thing you “gain” is confusion when trying to use SQL examples from anywhere else on the net. Just sayin’. :slight_smile:
PPS - After thinking about it I deleted the example code originally posted here because there is no good reason to use square brackets in a case like this – so I don’t want the sample code being used by someone else as “the way it should be done.” Just use the two-liner I posted above. [import]uid: 9440 topic_id: 14688 reply_id: 54330[/import]

(dupe) [import]uid: 9440 topic_id: 14688 reply_id: 54325[/import]

Sorry, I didn’t catch what you where saying with the brackets. But your example worked fine, thanks.

Dan [import]uid: 78446 topic_id: 14688 reply_id: 54333[/import]

Cool, glad you got it working.

Looking back I realize I didn’t *say* to ditch the brackets originally. :slight_smile:

Brackets are good for when you need to create a string that consists of multiple lines, but for a case like this where you’re making a single line of text, quotes are just simpler to use.

Jay
[import]uid: 9440 topic_id: 14688 reply_id: 54334[/import]