开发者

Html5 says that values are undefined

This is in a chrome extension and lets say I have this code:

var DB = openDatabase('CMPDB', '1.0', 'Database for CMP', 4 * 1024 * 1024); 
LoadFromDB();   

function LoadFromDB() {

    DB.transaction( function(tx)
    {           
        tx.executeSql('CREATE TABLE IF NOT EXISTS table(X, Y, Z UNIQUE)');

        tx.executeSql('SELECT * FROM table', [], function (tx, results) {
            var len = results.rows.length;开发者_如何学JAVA
            for (i = 0; i < len; i++) {                 
                alert(results.rows.item(i).X.text);
                alert(results.rows.item(i).Y.text);
                alert(results.rows.item(i).Z.text);                         

            }
        });
    });
}

Why do all of the alerts come back as undefined presuming that the table had been created prior and that the Dev tools on chrome say that the values are in the table?


I think your SELECT statement might be being called before the table has finished being created. Have you tried moving your SELECT query to a callback function so that it's called after your CREATE TABLE SQL has been run. Something like:

tx.executeSql('CREATE TABLE IF NOT EXISTS table(X, Y, Z UNIQUE)', function(tx) {
    tx.executeSql('SELECT * FROM table', [], function (tx, results) {
        var len = results.rows.length;
        for (i = 0; i < len; i++) {                 
            console.log(results.rows.item(i).X.text);
            console.log(results.rows.item(i).Y.text);
            console.log(results.rows.item(i).Z.text);                         
        }
    });            
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜