Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to avoid nested SELECT statements? #64

Open
EduVencovsky opened this issue Jan 14, 2019 · 2 comments
Open

How to avoid nested SELECT statements? #64

EduVencovsky opened this issue Jan 14, 2019 · 2 comments

Comments

@EduVencovsky
Copy link

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
Copy link

brodycj commented Jan 14, 2019

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
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants