Quantcast
Channel: Ionic Forum - Latest posts
Viewing all articles
Browse latest Browse all 229600

Ionic - Problems with SQLite

$
0
0

It works ! Thanks !
With this new way of doing it I don't really know how to use it in a function.

I try to do that :

export class DatabaseProvider {

  database: SQLiteObject;
  public names: String[] = [];

  constructor(public http: Http, private sqlite: SQLite) {

    (<any>window).plugins.sqlDB.remove("db.sqlite3",0, function deleteSuccess(){
      console.log('deleted')
    }, function deleteError(error){
      console.log('not deleted')
    });

    let self = this;
    (<any>window).plugins.sqlDB.copy("db.sqlite3", 0, function copySuccess(){
      console.log('copiedd')
      self.sqlite.create({
        name: 'db.sqlite3',
        location: 'default'
      }).then((db: SQLiteObject) => {
        this.database = db;
        db.executeSql("select * from table", {}).then((data) => {
        console.log(JSON.stringify(data))
        }).catch((e) => {
          console.log(JSON.stringify(e))
        });
      })
    })
  }

  getAll() {
    return this.database.executeSql("SELECT * FROM table", []).then((data) => {
      let names = [];
      if (data.rows.length > 0) {
        for (var i = 0; i < data.rows.length; i++) {
          names.push({ name: data.rows.item(i).name });
        }
      }
      return names;
    }, err => {
      console.log('Error: ', err);
      return [];
    });
  }

}

But by this way I have an error :
Unhandled Promise rejection: Cannot set property 'database' of null ; Zone: <root> ; Task: Promise.then ; Value: TypeError: Cannot set property 'database' of null

I don't see how to pass the database to my function.


Viewing all articles
Browse latest Browse all 229600

Trending Articles