将预加载的db文件放在Ionic 2 RC0中的位置 [英] Where to place pre loaded db file in Ionic 2 RC0

查看:263
本文介绍了将预加载的db文件放在Ionic 2 RC0中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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中,我从'ionic-native'导入 import {SQLite};

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:一个离子项目

这取自:在哪里将SQLite数据库放在Ionic 2 RC0上?:morris4ever69

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

这篇关于将预加载的db文件放在Ionic 2 RC0中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆