hello,
Sure. this is the complete code from my app which open and use a local database which is copied in /www at runtime by the snippet code you just put in angular.json from the previous message.
database: SQLiteObject;
private databaseReady: BehaviorSubject;
constructor(private storageService : StorageService, private sqlite: SQLite, private platform: Platform, private sqliteDbCopy: SqliteDbCopy) {
this.databaseReady = new BehaviorSubject(false);
this.platform.ready().then(() => {
this.storageService.getStorageSettings(Constants.DATABASE_FILLED).then(result => {
if(result) {
if(result != Constants.YES) {
this.sqliteDbCopy.copy(Constants.DATABASE_NAME, 1).then((res: any) => {
storageService.setStorageSettings(Constants.DATABASE_FILLED, Constants.YES).then(() => {
this.createAndOpenDB();
});
}).catch((error: any) => {
console.error(error)
});
} else {
this.createAndOpenDB();
}
} else {
this.sqliteDbCopy.copy(Constants.DATABASE_NAME, 1).then((res: any) => {
storageService.setStorageSettings(Constants.DATABASE_FILLED, Constants.YES).then(() => {
this.createAndOpenDB();
});
}).catch((error: any) => {
console.error(error)
});
}
});
});
}
createAndOpenDB() {
const options: any = {
name: Constants.DATABASE_NAME,
location: ‘default’,
createFromLocation: 1
};
this.sqlite.create(options).then((db: SQLiteObject) => {
this.database = db;
//this.loadProducts(); <- here you can call your sql methods. i will put an example of this as well below.
this.databaseReady.next(true);
}).catch(e => {
console.log(“error open db”, e);
});
}
getAllProducts() {
return this.database.executeSql(“SELECT * FROM product”, ).then((data) => {
let products = ;
if (data.rows.length > 0) {
for (var i = 0; i < data.rows.length; i++) {
products.push({
id: data.rows.item(i).product_id,
name: data.rows.item(i).product_name
});
}
}
return products;
}, err => {
console.log('Error: ', err);
return [ ];
});
}
PS. as you can see, you will have to use 2 plugins in order to use a local prepopulated database into a cordova-ionic project.
1st step is to copy the .db file from src/assets to /www with that rule from angular.json. (which you already did).
2nd step is to use only once (see my code above with that check on a storage memory app parameter which i called DATABASE_FILLED) the plugin cordova-plugin-dbcopy (link here: https://ionicframework.com/docs/native/sqlite-db-copy ). this plugin will only copy the .db file found in /www into the correct place where a .db file should be for an android/ios app. that’s it with it. I said only once because otherwise you will get an error like “DB ALREADY EXISTS”.
3rd step is to use the plugin cordova-sqlite-storage (link here: https://ionicframework.com/docs/native/sqlite ) to open the database and execute queries and on it.
From my code above, the StorageService is a local service written by me which uses the native storage plugin( link here: https://ionicframework.com/docs/native/native-storage) which holds small amount of key-value info into app’s memory. getStorageSettings()/ setStorageSettings() are methods which set and get values into memory with that plugin.
Please tell me that it works for you. When i developed that, i spent 2-3 days on it cuz i found nothing on the net and i understand your struggle.
All the best