0

Using IONIC 3 and sqlite3 v2 native, testing on Android device.

When I use one table everything is fine, but when I am trying to create 3 tables then I have a problem with other two.

database.ts (in providers):

...
db: any;
isOpen: Boolean = false;
constructor(private sqlite: SQLite) {}

createDatabase() {
    return new Promise((resolve, reject) => {
      this.sqlite.create({
        name: 'mydatabase.db',
        location: 'default'
      }).then((db: SQLiteObject) => {
        this.isOpen = true;
        resolve(db);
    }).catch(err => reject(err));
  });
}

openDb() {
return new Promise((resolve, reject) => {
  if (this.isOpen) {
    resolve({});
  } else {

    this.createDatabase().then(db => {
      this.db = db;
      this.db.executeSql("CREATE TABLE IF NOT EXISTS first (*my first query...)",{})
        .then(("CREATE TABLE IF NOT EXISTS second (*my second query...))"),{})
        .then(("CREATE TABLE IF NOT EXISTS third (*my third query...)"),{})
        .then(res => resolve(res))
        .catch(err=>reject(err));
    }).catch(err => reject(err));
  }
 });
}

error message:

sqlite3_prepare_v2 failure: no such table: second, code 5

second attempt:

openDb() {
return new Promise((resolve, reject) => {
  this.createDatabase().then(db => {
  this.db = db;      
  this.db.executeSql("CREATE TABLE IF NOT EXISTS first (firstquery...)",{})
      .then(()=>{this.db.executeSql("CREATE TABLE IF NOT EXISTS second (secondquery...)",{})})
    .then(res=>resolve(res))
      .catch(err=>reject(err));
    });

second then never triggered and only first table created.

I used many methods but this above it seems to be a solution nearest to my problem. Does this method seems right or I have to use something different, in order to make this work?

1 Answer 1

1

What executeSql returns is a promise telling whether the given query is successfully executed or not. If you wish to create another table only if the first table is created then you need to do as below

this.db.executeSql("CREATE TABLE IF NOT EXISTS first (*my first query...)",{})
.then(() =>{
this.db.executeSql("CREATE TABLE IF NOT EXISTS second (*my second query...))"),{}).then(()=>{
//Something else if required
}).catch(err => reject(err));
}).catch(err => reject(err));
Sign up to request clarification or add additional context in comments.

2 Comments

I have edit my question you will see it as my second attempt ...
The problem was somewhere else your solution works perfectly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.