每次我必须执行查询时都使用create for sqlite

时间:2017-11-26 20:27:18

标签: sqlite typescript ionic-framework ionic3

我正在学习在Ionic中使用SQLite。从我接下来的例子中,我已经提出了这个代码。

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');
        })
    })
  }

}

如果你看一下方法navigateToRegisterPage()我正在做一个SELECT,但我先做this.sqlite.create。每次我想执行查询时,是否必须这样做,还是有其他方法可以做到这一点?

0 个答案:

没有答案