1

我正在使用以下代码来获取 emp 表中的记录数,代码如下:

var myDb = initDB();  
    myDb.transaction(function(trans) {  
    var query = "SELECT COUNT(*) AS c from emp";  
        trans.executeSql(query, [], function(trans, res) {  
           var count = res.rows[0].c;  
           console.log("--- After Count ---"+count);  
     }, errorHandler); });

The query is giving error : Uncaught TypeError: Cannot read property 'c' of undefined.  
How to solve this issue?
Appreciate any help.
4

2 回答 2

0
html5.webdb.Count=function() {
    var db = html5.webdb.db;
  db.transaction(function (tx) {
    tx.executeSql('SELECT COUNT(*) AS c FROM yourtable', [], function (tx, r) {
      console.log( r.rows.item(0).c);
    }, function (tx, e) {
      alert ("unknown: " + e.message);
    });
  });
}

I use this in my db, thanks to James

于 2014-07-16T06:44:08.780 回答
0

我已经描述了下面的代码,它将始终有效以及访问 Count(*) 的正确方法

select COUNT(*) totalCount from tableName

因此您可以通过访问 totalCount 来访问返回计数

console.log(r.rows.item(0).totalCount);
于 2017-03-06T07:23:44.807 回答