Sqlite in Ionic

When i first Running my app its fine but when i close and open it gives error of plugin not found though plugin is installed in it.
When internet is on it works and if internet is off "Unable to open Database PLUG_IN_NOT_INSTALLED " Error.

How are you running your app?
How did you implement it? What is your code?
Also post your ionic info output please.

I am Running it in my android phone

export class HomePage {

private db: SQLiteObject;
firstname: any;
lastname: any;
items: Array<{ firstname: string, lastname: string }> = []

constructor(public navCtrl: NavController, public sqlite: SQLite) {

  if(this.items.length >0)
  {
     this.items.length=0
  }
      this.sqlite.create({
  name: "data.db",
  location: "default"
}).
  then((db: SQLiteObject) => {
    this.db = db;
    db.executeSql("CREATE TABLE IF NOT EXISTS people(id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT)", {})
      .then((data) => {
        console.log("TABLE CREATED: ", data);
      }, (error) => {
        console.error("Unable to execdb = window.sqlitePlugin.openDatabase({name: 'demo.db', location: 'default'});ute sql", error);
      })

     db.executeSql('select * from people', {}).then((data) => {
      if (data.rows.length > 0) {
        for (var i = 0; i < data.rows.length; i++) {
        
          console.log("==========================" + data.rows.item(i).firstname);

          this.items.push({ firstname:data.rows.item(i).firstname,lastname:data.rows.item(i).lastname });
        }
      }

    }, (error) => {
      alert('Unable to execute sql: ' + JSON.stringify(error));
    });




  }, (error) => {
    console.error("Unable to open database", error);
  });

}

insert() {

if(this.items.length >0)
  {
     this.items.length=0
  }

this.db.executeSql('INSERT INTO people(firstname,lastname) VALUES(?,?)', [this.firstname, this.lastname]).then(() => alert('Executed SQL'))
      .catch(e => console.log(e));

      this.db.executeSql('select * from people', {}).then((data) => {
    
      if (data.rows.length > 0) {
        for (var i = 0; i < data.rows.length; i++) {
        
          console.log("==========================" + data.rows.item(i).firstname);

          this.items.push({ firstname:data.rows.item(i).firstname,lastname:data.rows.item(i).lastname });
        }
      }

    }, (error) => {
      alert('Unable to execute sql: ' + JSON.stringify(error));
    });

}

ionViewDidLoad()
{
console.log("================Hurray I am Running First================================")

}

}

it runs well in second phone while both phone is samsung.

Please edit your post, it is not very readable at the moment.
Use the </> button above the input field to format your code, command line output or error message (select the text first, then click the button or wrap it in ``` manually). Check the preview if it looks better. This will make sure your text is readable and if it recognizes the programming language it also automatically adds code syntax highlighting. Thanks.

This has nothing to do with what phone you are running on. You have a race condition where you are potentially trying to select from a table before it has been created. Properly chain all your operations using then. Incidentally, the start of your constructor makes no sense. It’s impossible for this.items.length to be anything but zero at that point.

If there is problem in a code so why it running when internet is on.

`import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite'

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})

export class HomePage {

  private db: SQLiteObject;
  firstname: any;
  lastname: any;
  items: Array<{ firstname: string, lastname: string }> = []

  constructor(public navCtrl: NavController, public sqlite: SQLite) {

      if(this.items.length >0)
      {
         this.items.length=0
      }
          this.sqlite.create({
      name: "data.db",
      location: "default"
    }).
      then((db: SQLiteObject) => {
        this.db = db;
        db.executeSql("CREATE TABLE IF NOT EXISTS people(id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT)", {})
          .then((data) => {
            console.log("TABLE CREATED: ", data);
          }, (error) => {
            console.error("Unable to execdb = window.sqlitePlugin.openDatabase({name: 'demo.db', location: 'default'});ute sql", error);
          })

         db.executeSql('select * from people', {}).then((data) => {
          if (data.rows.length > 0) {
            for (var i = 0; i < data.rows.length; i++) {
            
              console.log("==========================" + data.rows.item(i).firstname);

              this.items.push({ firstname:data.rows.item(i).firstname,lastname:data.rows.item(i).lastname });
            }
          }

        }, (error) => {
          alert('Unable to execute sql: ' + JSON.stringify(error));
        });




      }, (error) => {
        console.error("Unable to open database", error);
      });

      
    
  }



  insert() {

    if(this.items.length >0)
      {
         this.items.length=0
      }

    this.db.executeSql('INSERT INTO people(firstname,lastname) VALUES(?,?)', [this.firstname, this.lastname]).then(() => alert('Executed SQL'))
          .catch(e => console.log(e));

          this.db.executeSql('select * from people', {}).then((data) => {
        
          if (data.rows.length > 0) {
            for (var i = 0; i < data.rows.length; i++) {
            
              console.log("==========================" + data.rows.item(i).firstname);

              this.items.push({ firstname:data.rows.item(i).firstname,lastname:data.rows.item(i).lastname });
            }
          }

        }, (error) => {
          alert('Unable to execute sql: ' + JSON.stringify(error));
        });
  }

  
  ionViewDidLoad()
  {
    console.log("================Hurray I am Running First================================")

  
  }


}`

Because the nature of race conditions is that they are unpredictable. Sometimes things work, and sometimes they don’t.

when i deploy first time it runs fine ,after close application and
start i found error Error: Uncaught (in promise): plugin_not_installed.
where is the problem i am beginner in Ionic

You should only call native functionality inside of platform.ready. Fix this and it will probably work (more often).

I will try it and let know you about it.

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
import { AlertController } from 'ionic-angular';
import { ActionSheetController } from 'ionic-angular'

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  db: SQLiteObject;

  items: Array<{ id: number, firstname: string, lastname: string }> = []
  constructor(public navCtrl: NavController,
    public sqlite: SQLite,
    public alertCtrl: AlertController,
    public actionSheetCtrl: ActionSheetController) {

  }

  ionViewDidLoad() {
    this.showdata();
  }


  add() {
    let alert = this.alertCtrl.create({
      title: 'Add Value',
      inputs: [
        {
          name: 'f_name',
          placeholder: 'First name',
        },
        {
          name: 'l_name',
          placeholder: 'Last name',
        }
      ],
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          handler: data => {
            console.log('Cancel clicked');
          }
        },
        {
          text: 'Save',
          handler: data => {

            if (data.f_name == '' && data.l_name == '') {

              let alert = this.alertCtrl.create({
                title: 'Warning',
                subTitle: 'All Field Must Required',
                buttons: ['ok']
              });
              alert.present();
            }
            else {
              this.sqlite.create({
                name: "data.db",
                location: "default"
              }).
                then((db: SQLiteObject) => {
                  db.executeSql('INSERT INTO usernameList(firstname,lastname) VALUES(?,?)', [data.f_name, data.l_name])
                    .then((data) => {
                      console.log("==========  Data Dat Data ===========", JSON.stringify(data))
                      console.log(data.f_name);
                      console.log(data.l_name);
                    })
                    .catch(e => console.log(e));
                })

              this.showdata();
            }
          }
        }
      ]
    });
    alert.present();
  }



  showdata() {

    if (this.items.length > 0) {
      this.items.length = 0
    }
    this.sqlite.create({
      name: 'data.db',
      location: 'default'
    })
      .then((db: SQLiteObject) => {
        db.executeSql('select * from usernameList', {}).then((data) => {
          // alert(JSON.stringify(data));
          this.items = [];
          if (data.rows.length > 0) {
            for (var i = 0; i < data.rows.length; i++) {

              this.items.push({ id: data.rows.item(i).id, firstname: data.rows.item(i).firstname, lastname: data.rows.item(i).lastname });
            }
          }
        })
      })
  }

  showOptions(id,fname,lname) {
    let actionSheet = this.actionSheetCtrl.create({
      title: 'Option',
      buttons: [
        {
          text: 'Update',
          handler: () => {
            console.log('Open clicked');

            let alert = this.alertCtrl.create({
              title: 'Update Value',
              inputs: [
                {
                  name: 'update_fname',
                  placeholder: 'First name',
                  value: fname,
                },
                {
                  name: 'update_lname',
                  placeholder: 'Last name',
                  value: lname,
                }
              ],
              buttons: [
                {
                  text: 'Dismiss',
                  role: 'cancel',
                  handler: data => {
                    console.log('Cancel clicked');
                  }
                },
                {
                  text: 'Update',
                  handler: data => {

                    if (data.update_fname == '' && data.update_lname == '') {

                      let alert = this.alertCtrl.create({
                        title: 'Warning',
                        subTitle: 'All Field Must Required',
                        buttons: ['ok']
                      });
                      alert.present();
                    }
                    else {
                      this.sqlite.create({
                        name: "data.db",
                        location: "default"
                      }).
                        then((db: SQLiteObject) => {
         db.executeSql("update usernameList set firstname = "+"'"+data.update_fname+"'"+",lastname ="+"'"+data.update_lname+"'"+" where ID="+id,{})
                            .then((data) => {
                              console.log("==========  Data Dat Data ===========", JSON.stringify(data))
        
                            })
                            .catch(e => console.log(e));
                        })

                      this.showdata();
                    }
                  }
                }
              ]
            });
            alert.present();
          }
        },
        {
          text: 'Delete',
          handler: () => {

            console.log('Delete clicked');


            this.sqlite.create({
              name: "data.db",
              location: "default"
            }).
              then((db: SQLiteObject) => {
                db.executeSql("DELETE FROM usernameList WHERE id =" + id, {})
                  .then(() => {
                    console.log("---------------------Data Deleted Successfully-----------------------------------------")
                    this.showdata();
                  })
                  .catch(e => console.log(e));
              })
          }
        },
        {
          text: 'Cancel',
          role: 'cancel',
          handler: () => {
            console.log('Cancel clicked');
          }
        }
      ]
    });

    actionSheet.present();
  }


}

Is There any problem in my code?