Where to put the pre-loaded db file in Ionic 2 RC0

I am using the sqlite cordova ext plugin to work with a pre-populated sqlite database.

With the new RC0 Ionic 2, the www folder is completely rebuilt with every build. You used to leave your db file in the www directory, since this is the default place for plugins to read, but now they delete and rebuild the entire www file and do not move the src files to www.

So, is there a new way that I can copy this db file to the www folder after building / preventing it from being deleted during assembly or any other work?

Error: handler failure without error handler: sqlite3_prepare_v2 failure: no such table: Table_Name (...)

My sqlite ext plugin config

this.db.openDatabase({ name: 'example.db', location: 'default', // the location field is required createFromLocation: 1, existingDatabase: true, }); 
+1
source share
1 answer

1) I installed the cordova-sqlite-ext plugin

2) In app.component.ts I imported import { SQLite } from 'ionic-native';

3) In the .ready () platform, I inserted:

 let db = new SQLite(); db.openDatabase({ name: "data.db", location: "default", createFromLocation: 1 }).then(() => { db.executeSql("SELECT * from config", []).then((data) => { console.log("Data received: ", data); }, (error) => { console.error("Unable to execute sql", error); }) }, (error) => { console.error("Unable to open database", error); }); 

4) I create a file called copy.config.json on the same package.json path and I inserted:

 module.exports = { include: [ { src: 'src/assets/', dest: 'www/assets/' }, { src: 'src/index.html', dest: 'www/index.html' }, { src: 'src/data.db', dest: 'www/data.db' }, { src: 'src/service-worker.js', dest: 'www/service-worker.js' }, { src: 'node_modules/ionic-angular/polyfills/polyfills.js', dest: 'www/build/polyfills.js' }, { src: 'node_modules/ionicons/dist/fonts/', dest: 'www/assets/fonts/' }, ] }; 

5) In the package.json file, I inserted:

 "config": { "ionic_copy": "./copy.config.js" }, 

before the last line of "description": "SqlProject: An Ionic project" .

This was taken from: Where do I host SQLite databases on Ionic 2 RC0? by: morris4ever69

0
source

Source: https://habr.com/ru/post/1271177/


All Articles