如何在javascript中从数据库表中访问这样的数据?

时间:2013-05-21 11:56:14

标签: javascript jquery cordova

我面临着非常特殊的问题。当我从服务器 - WEB控制台获取消息时,我想要的值是可见的。但是,当我把它用于循环时,它显示我05-22 18:58:23.203: I/Web Console(29392): JSCallback Error: TypeError: Cannot read property 'value' of undefined at file:///android_asset/www/cordova-2.1.0.js:3727的错误。 这意味着我的查询是正确的。我在循环中承认了一些用于检索数据的错误。

我不熟悉手机差距并使用JavaScript代码。我在执行时遇到疑问和问题。我在我的项目中使用sqlite数据库。

我的表结构几乎就是这样

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 的所有值,我应该如何编码呢?​​

我的查询:

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

因为,当我按照以下方式尝试时,

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个值

i = cid_value(cid_value = 2)以cid = 2为目标时,我得到以下内容

  

01-01 05:45:38.687:I / Web Console(2425):JSCallback:来自的消息   服务器:   SQLitePluginTransaction.queryCompleteCallback( '1104538538488000', '1104538538491000',   [{ “值”: “VALUE1”, “CID”: “2”},{ “值”: “value22”, “ID”: “6”, “CID”: “2”}]);   在file:///android_asset/www/cordova-2.1.0.js:3726。

问题是,当我从服务器获取消息作为JS Callback时,我得到的值。不是来自for循环

请建议我解决问题的方法!。引导我找到出路。

1 个答案:

答案 0 :(得分:2)

尝试这个,它应该工作(我没有测试过,以防我知道):

cid_value = "1";
try{
    tx.executeSql("SELECT cid , value FROM table where cid = ?;", [cid_value], success, failure);
}catch(e){
    console.log("error: "+e.message);
}
this.success = function(tx, results){
    if(results.rows.length > 0){
        for(var x = 0; x < results.rows.length; x++){
            var cidinstance = results.rows.item(x).cid;
            var valueinstance = results.rows.item(x).value;
            console.log("cid = "+cidinstance + " | valueinstance = "+valueinstance);
    }else{
        console.log("results length = 0");
    }
};
this.failure = function(tx, error){
    console.log("error: "+error.message)
}

我认为您的问题与您在for循环中初始化“i”ver的值有关。让它从0开始,得到结果集的每个结果,而不是它的一个子集。

希望有所帮助