HI,
I’m very new to Ionic and Angular so please forgive my noob question, but i’m trying to just open an SQLite database that i already have from my existing iOS and Android apps that i’ve written natively (now looking to use just 1 code base)
In my existing apps, i check to see if the DB exists and if it does i’ll just use that, if it doesn’t then i can download an updated version (if required) or copy it from the app bundle.
In Ionic it seems that i have to create a DB and import everything into it from my existing DB - Thats fine i can do that to, but when i next launch the app it copies everything back into the new DB instead of just using the existing data.
Can anyone shed any light on how i can do this. I’ve also tried to change the loaction of the new DB but i seem to only be able to use ‘deafult’…
My code is below & i’m only testing on an actual device (no web browser)
Any suggestions or pointers would be great!!
Thank you.
import { Platform } from '@ionic/angular';
import { Injectable } from '@angular/core';
import { SQLitePorter } from '@ionic-native/sqlite-porter/ngx';
import { HttpClient } from '@angular/common/http';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx';
import { BehaviorSubject, Observable } from 'rxjs';
import { File } from '@ionic-native/file/ngx';
import { LoadingController } from '@ionic/angular';
export interface Dev {
id: number,
name: string,
fav: string
}
@Injectable({
providedIn: 'root'
})
export class DatabaseService {
private database: SQLiteObject;
private dbReady: BehaviorSubject<boolean> = new BehaviorSubject(false);
readonly database_name: string = 'developers23.db';
developers = new BehaviorSubject([]);
products = new BehaviorSubject([]);
constructor(
private platform: Platform,
private sqlitePorter: SQLitePorter,
private sqlite: SQLite,
private http: HttpClient,
private file: File,
public loadingController: LoadingController
) {
this.platform.ready().then(() => {
console.log('Platform Ready!');
this.checkDBExists();
// this.createDB();
}).catch(error => {
console.log(error);
})
}
checkDBExists() {
this.file.checkFile('default', 'developers23.db')
.then(this.loadDevelopers)
.catch(this.createDB);
}
createDB() {
console.log('DB Create1');
this.sqlite.create({
name: this.database_name,
location: 'default'
})
.then((db: SQLiteObject) => {
this.database = db;
this.seedDatabase();
})
.catch(e => {
alert("error " + JSON.stringify(e))
});
}
seedDatabase() {
console.log('Seed database');
this.http.get('assets/seed3.sql', { responseType: 'text'})
.subscribe(sql => {
console.log('DB Create');
this.sqlitePorter.importSqlToDb(this.database, sql)
.then(_ => {
console.log('DB Create - then');
this.loadDevelopers();
this.dbReady.next(true);
})
.catch(e => console.error(e));
});
}
getDatabaseState() {
return this.dbReady.asObservable();
}
getDevs(): Observable<Dev[]> {
return this.developers.asObservable();
}
loadDevelopers() {
console.log('Getting Manufacturers');
return this.database.executeSql('SELECT * FROM Lighting_Manufacturers Order By Title COLLATE NOCASE', []).then(data => {
let developers: Dev[] = [];
console.log(data);
console.log('Getting Manufacturers');
if (data.rows.length > 0) {
for (var i = 0; i < data.rows.length; i++) {
developers.push({
id: data.rows.item(i)._id,
name: data.rows.item(i).Title,
fav: data.rows.item(i).fav
});
}
}
this.developers.next(developers);
});
}
1 post - 1 participant