0

pls help me i cant solve my problem on ionic 2 sqlite. on the first run of my app i have successfully created the table "players", and i can also insert and fetch on this table. but when i try to exit and re-open my app it prompts me "plugin_not_installed".

i am following the new documentation of ionic frame work . i observe on this documentation it doesn't have db.openDatabase() function which some tutorials always mention.

   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();
  }
}
lankean
  • 11
  • 5
  • additionally when i try to exit and re-open my app several times sometimes this error don't comes up. – lankean May 11 '17 at 06:11
  • can you add the code? [mcve] – Suraj Rao May 11 '17 at 06:17
  • hi sir @suraj thank you for your reply, how can i add the code? the comment box said it was too long – lankean May 11 '17 at 06:33
  • use [edit](http://stackoverflow.com/posts/43907767/edit) button..and `{}` button inside for adding code snippet – Suraj Rao May 11 '17 at 06:34
  • thanks , i already posted my code – lankean May 11 '17 at 06:37
  • when you call cordova plugins call within `this.platform.ready().then(()=>{})` – Suraj Rao May 11 '17 at 06:38
  • thank you sir @suraj i already did that. the results are the same. – lankean May 11 '17 at 06:47
  • i already added a loadingController to postpone somehow between the creation of tables and fetching but its the same. – lankean May 11 '17 at 06:48
  • try reinstalling the plugin `ionic plugin add --save` – Suraj Rao May 11 '17 at 06:52
  • hi sir @suraj , i already tried my luck on re moving and re-installing the sqlite plugin. actually its my 2nd dummy project created just to test this error. it's just keeping me wonder why sometimes it work and sometimes prompts my "plugin not installed" when i tried to exit and open my app. – lankean May 11 '17 at 06:57
  • I dont see any other issue with the code.. could be ionic-native related? – Suraj Rao May 11 '17 at 07:00
  • actually posting this problem to stackoverflow is my last resort. i already tried several methods to overcome this.i cant anymore continue my project because of this error im losing hope – lankean May 11 '17 at 07:01
  • maybe raise an issue in their github? is it device specific? – Suraj Rao May 11 '17 at 07:02
  • hi @suraj, yes my code works perfectly as it can insert and fetch my data. its just so annoying when i try to re open my app again i cant anymore view my list of players because it said "plugin_not_installed". im very thankful for your cooperation but im so sad i cant continue my work – lankean May 11 '17 at 07:02
  • hi sir @suraj, i didnt try this to other phones because i have only 1 smart android phone. maybe i will try to other devices. uhmmm, did you notice sir the documentation of ionic-framework im just wandering why it doesnt have db.openDatabase() function. – lankean May 11 '17 at 07:05
  • Encounteredd an exact same error how did you solve it? – konzo Jan 23 '18 at 10:25
  • 1
    This might help: https://stackoverflow.com/a/50270455/813951 – Mister Smith May 10 '18 at 10:16

1 Answers1

-1

install the SQLite plugin using the below command :

ionic cordova plugin add cordova-sqlite-storage --save

npm install --save @ionic-native/sqlite

Import the SQLite class to the app.module.ts file

import { SQLite, SQLiteObject } from '@ionic-native/sqlite';

providers: [SQLite, ...

and build in your device

Yassine Abid
  • 81
  • 2
  • 7