1

can you please tell me how to count the number of elements in a table in sqlite.It is not giving correct 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 Answers 4

3
var countSql = "SELECT count(*) AS cnt FROM users"; $cordovaSQLite.execute(db, countSql, []).then(function (res) { console.log("count : " + res.rows.item(0).cnt); }); 
Sign up to request clarification or add additional context in comments.

Comments

1

Try this code :

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 } 

I replaced db.transaction with db.readTransaction, added a COUNT(*) AS c instead of * and so I replaced results.rows.length with r.rows[0].c. For more information, you can have a look to the W3C documentation.

17 Comments

can you help in this question also..!! stackoverflow.com/questions/17752946/…
I don't understand. I answered to your question... Why also ?
Because i have one more problem biggest problem in this url stackoverflow.com/questions/17752946/… problem
check again..!! i have same problem in this question
|
1

You can use callBack function also..

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); }); }); } 

Call function like this..

Test('users',function(result_count){ alert(result_count); }); 

6 Comments

users is table name.? result_count number of element ?
is this apply on this question also .stackoverflow.com/questions/17752946/…
can we add check functionality if "test" not exist in db? then return 0.?
i don't do that i think by mistake it happen sorry ..where your answer gone..:(please paste again..
|
0

May use this

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); }); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.