SQLite question for Cordova app


#1

What is best practice for waiting to ensure all tables are created before continuing?

I am currently just nesting each create table in the then of the previous, like so.

$cordovaSQLite.execute(self.db, "CREATE TABLE IF NOT EXISTS table1(id integer primary key, field1 text, field2 tex)")
.then(function(res) {
    $cordovaSQLite.execute(self.db, "CREATE TABLE IF NOT EXISTS table2(id integer primary key, field1 text, field2 tex)")
      .then(function(res) {
          $cordovaSQLite.execute(self.db, "CREATE TABLE IF NOT EXISTS table3(id integer primary key, field1 text, field2 tex)")
         .then(function(res) {
              // return something here
          });
  });

});

I am asking because I built a phonegap app previously that used SQLite and there were problems with the creation of tables and inserting data into them, the creation sometime took long enough that it tried to insert before the table was ready.

so my question is this: is it possible to create the tables and not have to nest these functions but still ensure they are all finished before continuing? i haven’t seen any examples of this yet.


#2

You can chain promises.


#3

I will try this, thanks! I think this is what I was hoping for. That link also helped me understand how to handle things other than if everything goes perfectly.


#4

I am making some progress, but I try to open and init my db in the app.js like so

DBA.openDB()
  .then(function(results) {
    return results;
  })
  .then(function(result) {
    return DBA.init();
  });

But I get an error on logcat:
Uncaught TypeError: DBA.openDB(…).then is not a function.


#5

I think I get it now, I am missing some things in my functions, so then is not working unless the function is set up to do this.