问题
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 should I code it?
My query:
tx.executeSql("SELECT cid,value FROM table where cid="+cid_value, [], querySuccess, errorCB,cid_value);
because,when I tried it the following manner,
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 just 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,I am getting the values when I get the message from the server as JS Callback. not from the for loop
Please,suggest me ways to solve the problem!.guide me to find a way out.
回答1:
If you want to find the number of rows where a particular CID is listed, you need to use the COUNT function in SQLite.
So your statement would be ::
SELECT COUNT(values) FROM TableName WHERE cid=1
That will give you the count directly. You don't need any code to do the calculations yourself.
来源:https://stackoverflow.com/questions/16649454/how-to-find-the-length-of-a-particular-column-of-a-table-in-javascript