Ionic 3 complains that database is already open


#1

I’m using cordova-sqlcipher-adapter along with ionic-native/sqlite to carry out local DB operations. I’m using create method to open database every time I need to run executeSql for a DB operation. This makes Ionic 3 display the following message:

database already open: data.db

In another earlier topic on this forum, the user was using openDatabase() method to open the DB and hold a reference to the opened DB and carry out operations on the reference subsequently. However, it looks like openDatabase() method is not present as per ionic-native/sqlite documentation.

My question is, is it necessary to execute .create() every time I need to run .executeSql()? Is there a way to open the database ONCE and carry out operations on it at any time while the app is running?


#2

There is no need to run .create() every time to run .executeSql().
we can open database once in provider and carry out operation any time while app is running. like
export class YourProvider{
public db: any;
constructor(
private sqlite: SQLite
) {
}

initializeDatabase() {
this.sqlite.create({ name: “yourDbName”, location: “default” }).then((db: SQLiteObject) => {
this.db = db;
}

we can use the db instance for all operations while app is running as

export class Page{
constructor(
private yourProvider: YourProvider,
) {
}
this.yourProvider.db.executeSql();

hope this will help. :slight_smile: