在 Ionic 2 RC0 中放置预加载的 db 文件的位置

2021-12-15 00:00:00 sqlite cordova ionic-framework ionic2

我正在使用 sqlite cordova ext 插件来处理预填充的 sqlite 数据库.

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

在 Ionic 2 的新 RC0 中,www 文件夹在每次构建时都完全重建.以前,您会将 db 文件保留在 www 目录中,因为这是插件的默认读取位置,但现在它们会删除并重建整个 www 文件,并且不会将 src 文件移动到 www 中.

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.

那么有没有一种新方法可以在构建后将此 db 文件复制到 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?

错误:没有错误处理程序的语句失败:sqlite3_prepare_v2 失败:没有这样的表:Table_Name(…)

Error: a statement with no error handler failed: sqlite3_prepare_v2 failure: no such table: Table_Name(…)

我的 sqlite ext 插件配置

My sqlite ext plugin config

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

推荐答案

1) 我安装了cordova-sqlite-ext插件

1) I installed the cordova-sqlite-ext plugin

2) 在 app.component.ts 中我导入了 import { SQLite } from 'ionic-native';

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

3) 在我插入的 platform.ready() 中:

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) 我在 package.json 的同一路径创建了一个名为 copy.config.json 的文件,然后插入:

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) 在我插入的 package.json 文件中:

5) In the file package.json I inserted:

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

在最后一行之前"description": "SqlProject: An Ionic project".

这取自:将 SQLite 数据库放在 Ionic 2 RC0 的何处? 作者:morris4ever69

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

相关文章