‘addSQLiteSuffix’ does not copy the database. I am currently still running in ionic 4 with sqlite (ionic-native/sqlite) and here the database was stored by default in ‘Application/Library/LocalDatabase/’. In ionic capacitor 3.2.4 the database is stored by default in ‘Application/Documents/’ (here in IOS). I upload the app in simulator (IOS) with ionic 4 build and then with capacitor 3.2.4 build and the db is not copied. Can anyone help me out with this?
I have implemented the following (sorry, I’m still a beginner ):
async initializePlugin(): Promise {
this.platform = Capacitor.getPlatform();
const sqlitePlugin: any = CapacitorSQLite;
this.sqlite = new SQLiteConnection(sqlitePlugin);
this.isService = true;
}
async getEcho(value: string): Promise {
if(this.sqlite != null) {
this.setupDatabase();
return await this.sqlite.echo(value);
} else {
return null;
}
}
private async setupDatabase() {
const dbSetupDone = await Storage.get({ key: DB_SETUP_KEY });
if (!dbSetupDone.value) {
this.downloadDatabase();
} else {
this.addSQLiteSuffix();
this.interval = setInterval(() => {
if (this.timeLeft > 0) {
this.timeLeft--;
} else {
this.connectDB();
this.timeLeft = 3;
clearInterval(this.interval);
}
}, 500);
}
}
async connectDB() {
this.dbName = "testDB";
if( ! this.isDBConnected ) {
this.dbConnection = await this.createConnection( this.dbName, false, "no-encryption", 1);
}
await this.dbConnection.open();
}
async createConnection( database: string, encrypted: boolean, mode: string, version: number ): Promise<SQLiteDBConnection | null> {
let statusDB = (await this.sqlite.isConnection(this.dbName )).result;
if (this.sqlite != null) {
const db: SQLiteDBConnection = await this.sqlite.createConnection( database, encrypted, mode, version );
if (db != null) {
this.isDBConnected = true;
return Promise.resolve(db);
} else {
return Promise.reject(new Error(`no db returned is null`));
}
} else {
return Promise.reject(new Error(`no connection open for ${database}`));
}
}
private downloadDatabase() {
this.http.get('./assets/db/db.json')
.subscribe(async (jsonExport: JsonSQLite) => {
const jsonstring = JSON.stringify(jsonExport);
const isValid = await CapacitorSQLite.isJsonValid({ jsonstring });
if (isValid.result) {
this.dbName = jsonExport.database;
await Storage.set({ key: DB_NAME_KEY, value: this.dbName });
await CapacitorSQLite.importFromJson({ jsonstring });
await Storage.set({ key: DB_SETUP_KEY, value: '1' });
this.dbReady.next(true);
}
});
}
async addSQLiteSuffix() {
let directory: string = "default";
if(this.platform === "ios") directory = "Library/LocalDatabase";
if(this.platform === "android" ) directory = "files/databases";
await this.sqlite.addSQLiteSuffix(directory);
this.getDatabaseList();
}
I want to avoid that the saved data of the users is gone at the next update, because it was technically not possible to migrate it…
Thanks already
Best regards