javascript - JS function to count rows in SQLite table - Stack Overflow

I'm trying to make a JS function that counts rows in an SQLite table.function countRows(){db.trans

I'm trying to make a JS function that counts rows in an SQLite table.

function countRows(){
    db.transaction(function (tx){
        tx.executeSql('SELECT id FROM table', [], function (tx, results) {
            var len = results.rows.length;
            alert(len);
        });
    });
}

The above code displays an alert with numbers of rows in the table. However, I'd like to make a function that would return the number instead of showing the alert box.

I tried:

function countRows(){
    db.transaction(function (tx){
        tx.executeSql('SELECT id FROM table', [], function (tx, results) {
            var len = results.rows.length;
            return len;
        });
    });
}

And then:

var number = countRows();
alert (number); // returns "undefined"

The above example returns "undefined", whereas a parallel example works fine:

function count(){
    return 3;
}
var number = count();
alert (number); // returns 3

I want to assign the number to a variable, so I could then make another sql query, count rows in another table, and pare the two results.

In PHP this would be:

$sql1 = mysql_query('SELECT COUNT(*) FROM table1');
$rows1 = mysql_result($sql1, 0);
$sql2 = mysql_query('SELECT COUNT(*) FROM table2');
$rows2 = mysql_result($sql2, 0);
if ($row1>$row2){}

I'm trying to make a JS function that counts rows in an SQLite table.

function countRows(){
    db.transaction(function (tx){
        tx.executeSql('SELECT id FROM table', [], function (tx, results) {
            var len = results.rows.length;
            alert(len);
        });
    });
}

The above code displays an alert with numbers of rows in the table. However, I'd like to make a function that would return the number instead of showing the alert box.

I tried:

function countRows(){
    db.transaction(function (tx){
        tx.executeSql('SELECT id FROM table', [], function (tx, results) {
            var len = results.rows.length;
            return len;
        });
    });
}

And then:

var number = countRows();
alert (number); // returns "undefined"

The above example returns "undefined", whereas a parallel example works fine:

function count(){
    return 3;
}
var number = count();
alert (number); // returns 3

I want to assign the number to a variable, so I could then make another sql query, count rows in another table, and pare the two results.

In PHP this would be:

$sql1 = mysql_query('SELECT COUNT(*) FROM table1');
$rows1 = mysql_result($sql1, 0);
$sql2 = mysql_query('SELECT COUNT(*) FROM table2');
$rows2 = mysql_result($sql2, 0);
if ($row1>$row2){}
Share Improve this question edited Mar 22, 2012 at 15:14 lukeshek asked Mar 22, 2012 at 14:13 lukesheklukeshek 9782 gold badges9 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You would be served much better by changing your query.

SELECT COUNT(ID) FROM table

Plus, its an async call so that the call return is returning the callback function. You should pass your own callback function.

function countRows(callback){
    db.transaction(function (tx){
        tx.executeSql('SELECT id FROM table', [], function (tx, results) {
            var len = results.rows.length;
            callback(len);
        });
    });
}

db.transaction is asynchronous. returning value isn't assigned to any variable in code above. Solution is to pass callback or create custom event, which is almost the same.

Something like this:

function countRows(cb){
    db.transaction(function (tx){
        tx.executeSql('SELECT id FROM table', [], function (tx, results) {
            var len = results.rows.length;
            cb.call(this, len);
        });
    });
}

countRows(function (num) {alert(num)});

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

相关推荐

  • javascript - JS function to count rows in SQLite table - Stack Overflow

    I'm trying to make a JS function that counts rows in an SQLite table.function countRows(){db.trans

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信