dimanche 3 juillet 2016

Using JavaScript in a HTML5 page to connect to a SQLite database

I'm trying to make a simple word game that has a database of words (which is preloaded or can be loaded by the user) that can be updated through website. I found this example and I got it to work. However, it wont work if I have an existing database. I'm new to this type of programming, so i've been doing research for 2 days now.

var db = openDatabase('db.sqlite', '1.0', 'Test DB', 2 * 1024 * 1024);
var msg;
/*
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS vocabList(id unique, word, definition, weekNO)');
msg = '<p>Log message created and row inserted.</p>';
document.querySelector('#status').innerHTML =  msg;
});
*/ 

I commented this out to prevent creating the database (should i have not done that?)

db.transaction(function(tx) {
    tx.executeSql('SELECT * FROM vocabList WHERE weekNO = 1', [], function(tx, results) {
        var len = results.rows.length,
            i;
        msg = "<p>Found rows: " + len + "</p>";
        document.querySelector('#status').innerHTML += msg;
        for (i = 0; i < len; i++) {
            msg = "<p><b>" + results.rows.item(i).word + "</b></p>";
            document.querySelector('#status').innerHTML += msg;
        }
    }, null);
});

If there is another more efficient way to do this, please point me in the right direction.

Aucun commentaire:

Enregistrer un commentaire