Sreehari
Sreehari

Reputation: 515

how to pass parameters in sqlite transaction in cordova

I have a bookid and i need to pass it on sqlite transaction in cordova like

var bookName  = db.transaction(function(tx){
                    return tx.executeSql('SELECT * FROM Iqra_Book WHERE book_id=?',[1],function(tx,res){
                        var leng = res.rows.length;
                        console.log("Length: " + leng);
                        return bkName = res.rows.item(0).book_name;
                    }
                , function(e) {
                    return console.log("ERROR: " + e.message);
                });
            });

But I am getting this booName as undefined. Can anyone help me out ?

Upvotes: 2

Views: 2730

Answers (2)

Frank Lewis
Frank Lewis

Reputation: 46

You should be using the values parameter of the executeSql() method.

eg.

let id = //the ID of the item you are looking for
tx.execute('SELECT * FROM Iqra_Book WHERE book_id=?',[id])
     .then(queryResult => {
       console.log(queryResult.rows.item(0).book_name)
     }).catch(err => console.error(`ERROR: ${JSON.stringify(err)}`);

NOTE

  • item() is a function - you have that right, it cannot be accessed as an array as is shown in the previous answer (I hate that about the plugin, but that is how it is)
  • the values passed in the previous example is just 1, so I hope the ID you wanted was 1.
  • queryResult.rows is a list-like object, I would recommend going through a for-loop with the limit of queryResult.rows.length unless (like in this case) you know for sure there is only going to be 1 result.

Upvotes: 0

最白目
最白目

Reputation: 3644

you need to pass a variable to the book id. leave away the "return", you cant just return a value since the web database works asynchrounously

tx.executeSql('SELECT * FROM Iqra_Book WHERE book_id ="'+id+'"',[1],function(tx,res){

also, change the line

return bkName = res.rows[0].book_name;

to

return bkName = results.rows.item[0].book_name;

and check if it works

Upvotes: 2

Related Questions