【问题标题】:ionic 2 sqlite plugin_not_installed离子 2 sqlite plugin_not_installed
【发布时间】:2017-10-10 00:07:00
【问题描述】:

请帮助我,我无法解决我在 ionic 2 sqlite 上的问题。在我的应用程序的第一次运行中,我成功创建了“玩家”表,我还可以在该表上插入和获取。但是当我尝试退出并重新打开我的应用程序时,它会提示我“plugin_not_installed”。

我正在关注 ionic frame work 的新文档。我在这个文档中观察到它没有一些教程总是提到的 db.openDatabase() 函数。

   import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
import { AlertController } from 'ionic-angular';
export class Players {
    name:string;    //ngmodel from html
    players:new Array<Object>(); //used to list all players
constructor(private sqlite: SQLite,public alertCtrl: AlertController) { 
this.initializeDatabase(); //create table if not exists

}
ionViewDidLoad(){
        this.fetch();
}
initializeDatabase(){
    this.sqlite.create({
  name: 'data.db',
  location: 'default'
})
  .then((db: SQLiteObject) => {
    db.executeSql('create table if not exists(playerId integer primary key,fullname varchar(50));', {})
      .then(() => console.log('Executed SQL'))
      .catch(e => this.showAlert( JSON.stringify(e)));
  })
  .catch(e => this.showAlert( JSON.stringify(e)));
}
fetch(){
        let sql="select fullname from players";
        this.sqlite.create({
  name: 'data.db',
  location: 'default'
}).then((db: SQLiteObject) => {
    db.executeSql(sql, {})
      .then((data) => {
          this.players=[];
          for(let index=0;index,data.rows.length;index++){
              this.players.push({fullname:data.rows.item(index).fullname});
          }
      })
      .catch(e => this.showAlert( JSON.stringify(e)));
  })
  .catch(e => this.showAlert( JSON.stringify(e)));
}
create(){
    let name=this.name;
        let sql="INSERT INTO players (playerId,fullname) values (?,?)";
        this.sqlite.create({
  name: 'data.db',
  location: 'default'
}).then((db: SQLiteObject) => {
    db.executeSql(sql, [null,name])
      .then(() => console.log('successfully created'))
      .catch(e => this.showAlert( JSON.stringify(e)));
  })
  .catch(e => this.showAlert( JSON.stringify(e)));
}
  showAlert(msg:string) {
    let alert = this.alertCtrl.create({
      title: 'Info',
      subTitle:msg,
      buttons: ['OK']
    });
    alert.present();
  }
}

【问题讨论】:

  • 另外,当我多次尝试退出并重新打开我的应用程序时,有时不会出现此错误。
  • 可以添加代码吗? minimal reproducible example
  • 嗨,@suraj 先生,谢谢您的回复,我该如何添加代码?评论框说太长了
  • 使用edit按钮..和{}里面的按钮添加代码sn-p
  • 这可能会有所帮助:stackoverflow.com/a/50270455/813951

标签: angularjs sqlite typescript ionic2


【解决方案1】:

使用以下命令安装 SQLite 插件:

ionic cordova 插件添加 cordova-sqlite-storage --save

npm install --save @ionic-native/sqlite

将 SQLite 类导入到 app.module.ts 文件中

从 '@ionic-native/sqlite' 导入 { SQLite, SQLiteObject };

提供者:[SQLite,...

并在您的设备中构建

【讨论】:

  • 因为 lankean 伤心,第一次运行应用并创建了表格,所以安装了插件...
猜你喜欢
  • 1970-01-01
  • 2019-02-14
  • 2019-05-08
  • 1970-01-01
  • 2017-08-30
  • 2016-09-02
  • 2019-01-10
  • 2016-09-30
  • 2016-08-16
相关资源
最近更新 更多