Where to place pre loaded db file in Ionic 2 RC0

2019-04-02 16:22发布

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

With the new RC0 of Ionic 2 the www folder is completely rebuilt on every build. Previously you would leave your db file in the www directory as this is the plugins default location to read from, but now they remove and rebuild the whole www file and don't move src files into www.

So is there a new way I can copy this db file into the www folder after build/prevent it from being removed on builds or any other work around?

Error: a statement with no error handler failed: 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条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-02 16:46

1) I installed the cordova-sqlite-ext plugin

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

3) In the platform.ready() 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 at the same path of package.json 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 file package.json I inserted:

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

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

This was taken from: Where to put SQLite databases on Ionic 2 RC0? by: morris4ever69

查看更多
登录 后发表回答