How to check if Ionic 2 SQLite Native database is already open


#1

I am using ionic 2 sqlite and I am trying to check if database is already open.

I know I can do

import { SQLite } from 'ionic-native';
...

const vm = this;
vm.db = new SQLite();
db.openDatabase({
  name: 'data.db',
  location: 'default'
}).then(() => {
  // The database opened successfully
}, (err) => {
  // Unable to open database;
  // The database could already be already open
});

So the logic that I want implement is like the following

if(db.isOpen) {
  query()
} else {
  openDatabase().then(()=> {
    query()
  })
}

query() {
  db.executeSql( ...
}

#2

See SQLite database already open: data.db