开发者

Get number of records in table

I have following piece of code, which should be able to log to console number of records (if table subjects exists).

db = openDatabase("myDatabase", "1.0", "", 200000);

db.transaction(function(tx) {
    tx.executeSql("SELECT COUNT(*) AS nor FROM subjects", [],
        function(result){
            console.log(result.rows);
        },
        function(tx, error){
            tx.executeSql("CREATE TABLE subjects (id REAL UNIQUE, name TEXT)");
        }
    );
});

Actually the result.rows is logging as undefined, so i even cannot call method item(int index) on it. How can i 开发者_StackOverflow中文版get access to number of returned records?


You forgot the tx parameter in your onSuccess callback function. Your result variable actually is the transaction object.

Here’s the corrected code:

db = openDatabase("myDatabase", "1.0", "", 200000);

db.transaction(function(tx) {
    tx.executeSql("SELECT COUNT(*) AS nor FROM subjects", [],
        function(tx, result){ // <-- this is where you forgot tx
            console.log(result.rows);
        },
        function(tx, error){
            tx.executeSql("CREATE TABLE subjects (id REAL UNIQUE, name TEXT)");
        }
    );
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜