How to find the length of a specific table column in javascript?

I am using sqlite database in my project.

My table structure will be almost like this

id cid values 1 1 value1 2 1 value2 3 1 value3 4 1 value4 5 2 value1 6 2 value2 7 3 value1 8 3 value2 9 3 value3 

so I want all the values of the same cid, how do I encode?

My request:

 tx.executeSql("SELECT cid,value FROM table where cid="+cid_value, [], querySuccess, errorCB,cid_value); 

because when I tried this,

 var details = results.rows.length; console.log("db test value:"+results.rows.item(cid_value).value); for (var i=cid_value; i<details; i++) { cidinstance=results.rows.item(i).cid; var valueinstance=results.rows.item(i).value; document.getElementById("s"+i+"cause").innerHTML=valueinstance; console.log("cid= "+cidinstance + "valueinstance= "+valueinstance); } 

then when i = cid_value (cid_value = 1) targeting cid = 1, where we have four values, I get only 3 values . but if I put i = 0 then i get 4 values

when i = cid_value (cid_value = 2) targeting cid = 2, I get the following

 01-01 05:45:38.687: I/Web Console(2425): JSCallback: Message from Server: SQLitePluginTransaction.queryCompleteCallback('1104538538488000','1104538538491000', [{"value":"value1","cid":"2"},{"value":"value22","id":"6","cid":"2"}]); at file:///android_asset/www/cordova-2.1.0.js:3726. 

The problem is that I get values ​​when I receive a message from the server as JS Callback. not out of for loop

Please suggest me ways to solve the problem! .Guide me to find a way out.

+1
source share
1 answer

If you want to find the number of rows that contain a specific CID, you need to use the COUNT function in SQLite.

So your statement will be:

 SELECT COUNT(values) FROM TableName WHERE cid=1 

This will give you an account directly. You do not need any code to perform the calculations yourself.

+1
source

All Articles