如何在JavaScript中查找表格中特定列的长度? [英] How to find the length of a particular column of a table in javascript?
问题描述
我在项目中使用sqlite数据库.
I am using sqlite database in my project.
我的表结构几乎像这样
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
所以我想要同一个 cid 的所有值,我应该如何编码?
so I want all the values of the same cid,how should I code it?
我的查询:
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);
}
然后,当i = cid_value(cid_value = 1)定位到cid = 1时,我们有四个值,我得到的只是 3个值.但是,如果我输入 i = 0 ,那么我得到 4个值
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
当 i = cid_value(cid_value = 2)定位到cid = 2时,我得到以下信息
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.
问题是,当我从服务器获取消息作为JS回调时,我正在获取值.不是来自for循环
请,建议我解决问题的方法!指导我找到出路.
Please,suggest me ways to solve the problem!.guide me to find a way out.
推荐答案
如果要查找列出特定CID的行数,则需要在SQLite中使用COUNT函数.
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.
这篇关于如何在JavaScript中查找表格中特定列的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!