storesafe / cordova-sqlite-storage-help

Help forum for Cordova sqlite plugin projects
2 stars 1 forks source link

How to avoid nested SELECT statements? #64

Open EduVencovsky opened 5 years ago

EduVencovsky commented 5 years ago

How can I avoid nested SELECT's when I need the result of one select to make another select? Is there a pratical way of doing it?

db.transaction( (tx) => {
  tx.executeSql(SELECT, [params], (tx, res1) => {
  //some operation
    tx.executeSql(SELECT, [res1], (tx, res2) => {
    //some operation
      tx.executeSql(SELECT, [res2], (tx, res3) => {
      //some operation
        ...
       })
     })
  })
})
brodycj commented 5 years ago

You would probably want to use subqueries. Here are a few resources I found:

Also note that tx.execureSql() operations are executed in the order they are given in a single function (db.transaction or tx.executeSql callback function).

For further help I would suggest you give a more specific example of what you need to do.

Private, commercial support is also available, please contact sales@litehelpers.net in case you are interested.

Note that this issue was transferred from cordova-sqlite-storage to cordova-sqlite-help. I hope to document this someday.

EduVencovsky commented 5 years ago

I found a small example in the documentation of what I'm trying to avoid in my code.

...
  tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) {
      console.log("insertId: " + res.insertId + " -- probably 1");
      console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");   

      tx.executeSql("select count(id) as cnt from test_table;", [], function(tx, res) {
        console.log("res.rows.length: " + res.rows.length + " -- should be 1");
        console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");
      });
...

Here is a small example of a nested SQL statements, it's only one inside the other, but what If you have more complex logic after the SELECT

What I really want to know is: Is there a way of getting the result of a statement, and using it without having to nest it?