0

你能告诉我如何计算number表格中的元素数量吗?sqlite它不正确result

function Test(test){
alert(test)
var x;
    db.transaction(function (tx) {
            $yoursql = 'SELECT * FROM  "'+test+'"';
            tx.executeSql($yoursql, [], function (tx, results) {
                alert(results.rows.length+ "rows")
                x= results.rows.length+"TableName"+test;

            });
            return x;
});
4

4 回答 4

2
var countSql = "SELECT count(*) AS cnt FROM users";
$cordovaSQLite.execute(db, countSql, []).then(function (res) {
    console.log("count : " + res.rows.item(0).cnt);
});
于 2015-03-24T11:35:43.723 回答
1

试试这个代码:

function Test(test){
    alert(test)
    var x;
    db.readTransaction(function (t) {
        t.executeSql('SELECT COUNT(*) AS c FROM ' + test, [], function (t, r) {
            alert(r.rows[0].c + "rows")
            x= r.rows[0].c+"TableName"+test;
        });
    });
    return x
}

我替换db.transactiondb.readTransaction,添加了一个COUNT(*) AS c而不是,*所以我替换results.rows.lengthr.rows[0].c。有关更多信息,您可以查看W3C 文档

于 2013-07-20T06:06:17.633 回答
1

您也可以使用回调函数..

function Test(test, callBack){
    var x;
    db.transaction(function (tx) {
            $yoursql = 'SELECT * FROM  "'+test+'" ';
            tx.executeSql($yoursql, [], function (tx, results) {
                x = results.rows.length  + "TableName" + test;
                callBack(x);                
            });
    });

}

像这样调用函数..

Test('users',function(result_count){
  alert(result_count);
});
于 2013-07-20T06:20:49.197 回答
0

可以用这个

var dBase = null;
function devicereadyFun(){
    dBase = window.sqlitePlugin.openDatabase({
        name: 'test.db',
        location: 'default'
    });
}

document.addEventListener('deviceready', devicereadyFun, false);

function Test(test){
    dBase.executeSql("select count(*) as cut from "+test, [], function(rsp){
        alert('Rows count in '+test+': '+rsp.rows.item(0).cut);
    });
}
于 2018-01-24T10:40:26.430 回答