简体   繁体   中英

how to access such a data from the database table in javascript?

THE PROBLEM I AM FACING VERY PECULIAR.THE VALUES WHAT I WANT IS VISIBLE,WHEN I GET IT AS MESSAGE FROM THE SERVER-WEB CONSOLE. BUT,WHEN I GIVE IT IN FOR LOOP,IT SHOWS ME AN ERROR OF 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 . THIS MEANS MY QUERY IS RIGHT.I HAVE COMMITTED SOME MISTAKE IN THE FOR LOOP FOR RETRIEVING DATA.

I am new to phone gap and using JavaScript to code.I have a doubt and a problem while executing it.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.

try this, it should work (I haven't tested it, in case let me know):

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

I think your problem is related to the value you initialize your "i" ver in the for cycle. let it start from 0 to get every result of the resultset, instead of a subset of it.

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM