Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using webkit database storage for highscores

I just made a simple HTML5 game for mobile webkit. I have made a simple function that inserts the record into the database:

Javascript:

db = openDatabase('highscores', '1.0', 'Whackem Highscores', 2 * 1024 * 1024);
db.transaction(function (tx) {
      tx.executeSql('CREATE TABLE scores (id unique, int,text)');
      tx.executeSql('INSERT INTO foo (id, text) VALUES (?,?,?)',["",score,date1]);
});

It doesn't seem to do anything!?

How can I fix this?

I also have to make a loop that prints out the highest scores, If you have any advice on something like that, it would also be very helpful.

like image 637
Ben Avatar asked Jul 17 '26 14:07

Ben


1 Answers

Use localStorage instead. It will be much simpler to use (assuming you aren't going to store hundreds of thousands highscores on each client).

Also, WebSQL has been deprecated in favor of IndexedDB (although the latter is not in shipping browsers yet).

localStorage is simply a persistent object that holds strings, so you can feed it JSON with all your (small) data.

localStorage['highscores'] = JSON.stringify(your_highscores_table);

Reading:

your_highscores_table = JSON.parse(localStorage['highscores']);

Or you could split it using different keys rather than lumping everything in 'highscore'.

like image 79
Kornel Avatar answered Jul 19 '26 02:07

Kornel