Using JavaScript in a HTML5 page to connect to a SQLite database - Stack Overflow

I'm trying to make a simple word game that has a database of words (which is preloaded or can be l

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 mented 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.

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 mented 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.

Share Improve this question edited Oct 2, 2014 at 20:38 Josue Espinosa 5,08916 gold badges50 silver badges83 bronze badges asked Dec 1, 2011 at 8:00 jester113jester113 241 gold badge1 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 0

I'm not exactly sure what you're having problems with. But in your code, you mented out the part where it checks whether or not a table has been created. From that mented out code, there are no rows being inserted either, so it is a blank database.

I modified your code here: http://jsfiddle/Ae8m2/

I unmented out your creation part, and added a couple rows.

The database you are creating/connecting to is a client-side database. If you want to connect to a back-end database, you should use a server language, such as php.

You may want to use ajax with this, to connect to your database and execute your requests.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745146218a4613659.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信