【问题标题】:Create SQLite database on sdcard in ionic3在 ionic3 的 sdcard 上创建 SQLite 数据库
【发布时间】:2018-06-19 23:56:29
【问题描述】:

我正在尝试使用带有 ionic3 的原生 @ionic-native/sqlite 模块。如果我在默认位置创建一个 SQLite 数据库,那么数据库就会被创建。

  constructor( public sqlite: SQLite) {
    this.initDatabase();

  }


  initDatabase(){

    config = {
      name: 'offline.db',
      location: 'default'
    }
    this.sqlite.create(this.config).then(async  (db: SQLiteObject) => {
      await db.executeSql(`
      CREATE TABLE IF NOT EXISTS cond_pag (
          id_codizione_pag varchar(4),
          descrizione varchar(150),
          CONSTRAINT cond_pag_pk PRIMARY KEY (id_codizione_pag)  
       )`,{});

      })
      .catch(e => {
        console.log(e);
      });    

  }


}

但如果我尝试使用 sd card insetad

config = {
      name: 'offline.db',
      location: '/sdcard'
}   

ionic 抛出错误

Error: Valid iOS database location could not be determined in openDatabase call
    at newSQLError (SQLitePlugin.js:26)
    at Object.<anonymous> (SQLitePlugin.js:581)
    at Object.openDatabase (SQLitePlugin.js:59)
    at index.js:199
    at new t (polyfills.js:3)
    at SQLite.create (index.js:198)
    at SQLite.value [as create] (decorators.js:49)
    at DatabaseProvider.webpackJsonp.200.DatabaseProvider.initDatabase (database.ts:28)
    at new DatabaseProvider (database.ts:20)
    at _createClass (core.js:10933) 

缺少什么?

【问题讨论】:

  • 首先您需要将文件写入sdcard的权限(使用离子诊断插件获取权限),接下来我认为您应该指定iosDatabaseLocation关注link
  • 感谢 NRaghavendra。我尝试为 Android 平台添加用户权限,但收到相同的消息。我找到了另一个出路。我将 ionic 配置为编译到 Android Studio 项目的资产目录中。我创建了一个基于 WebView 组件的应用程序。现在我可以从 TypeSpript 访问本机函数,而 Java-Android 完成了这项肮脏的工作。我的客户只想要 Android 版本。

标签: android sqlite ionic-framework


【解决方案1】:

以下代码对我有用

initDatabase(){
        config = {
          name: '/sdcard/offline.db', // file:///sdcard/offline.db
          location: 'default'
        }
        this.sqlite.create(this.config).then(async  (db: SQLiteObject) => {
          await db.executeSql(`
          CREATE TABLE IF NOT EXISTS cond_pag (
              id_codizione_pag varchar(4),
              descrizione varchar(150),
              CONSTRAINT cond_pag_pk PRIMARY KEY (id_codizione_pag)  
           )`,{});

          })
          .catch(e => {
            console.log(e);
          });    

      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多