I've been diving into SQLite within the Ionic framework and have pieced together some code based on examples I've encountered.
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
@IonicPage()
@Component({
selector: 'page-sqlite',
templateUrl: 'sqlite.html',
})
export class SqlitePage {
constructor(public navCtrl: NavController, public navParams: NavParams, public sqlite: SQLite) {
this.sqlite.create({
name: 'data.db',
location: 'default'
})
.then((db: SQLiteObject) => {
db.executeSql('create table if not exists danceMoves(name VARCHAR(32))', {})
.then(() => console.log('Executed SQL'))
.catch(e => console.log(e));
})
.catch(e => console.log(e));
}
ionViewDidLoad() {
console.log('ionViewDidLoad SqlitePage');
}
navigateToRegisterPage(){
this.sqlite.create({
name: 'data.db',
location: 'default'
})
.then((db: SQLiteObject) => {
db.executeSql('SELECT * FROM danceMoves', [])
.then(res => {
console.log(res);
console.log('The SELECT has been succesfully executed');
})
})
}
}
In the navigateToRegisterPage()
function, I perform a SELECT query after calling this.sqlite.create
. Do I need to do this every time I want to execute a query, or is there a more efficient approach?