离子2 sqlite plugin_not_installed

时间:2017-05-11 06:10:48

标签: angularjs sqlite typescript ionic2

请帮助我,我无法解决离子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();
  }
}

1 个答案:

答案 0 :(得分:-1)

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

  

离子cordova插件添加cordova-sqlite-storage --save

     

npm install --save @ ionic-native / sqlite

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

  

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

     

提供商:[ SQLite ,...

并在您的设备中构建