在ionic3中的sdcard上创建SQLite数据库

时间:2018-06-19 23:56:29

标签: android sqlite ionic-framework

我正在尝试将本机@ ionic-native / sqlite模块与ionic3一起使用。如果我在默认位置创建一个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卡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) 

缺少什么?

1 个答案:

答案 0 :(得分:0)

以下代码对我有用

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);
          });    

      }