在Ionic 3

时间:2018-01-29 09:56:10

标签: angular sqlite ionic-framework

对不起,我是新来的。我正在制作一个应用程序,假设在离线时将有关建筑物的信息添加到数据库中,并在手机上线时与服务器同步。我想在我的离子项目中使用外部sqlite db。我如何连接到外部数据库。

1 个答案:

答案 0 :(得分:1)

安装Cordova和Ionic Native插件:

$ ionic cordova plugin add cordova-sqlite-storage

$ npm install --save @ionic-native/sqlite

安装插件包后,将其添加到您应用的NgModule。

以下是在您的应用中使用sqlite保存数据的示例代码

saveData() {
    this.sqlite.create({
      name: 'myionicdb.db', // Name of the Database
      location: 'default'
    }).then((db: SQLiteObject) => {
      db.executeSql('INSERT INTO expense VALUES(NULL,?,?,?,?)',[this.data.date,this.data.type,this.data.description,this.data.amount])
        .then(res => {
          console.log(res);
          this.toast.show('Data saved', '5000', 'center').subscribe(
            toast => {
              this.navCtrl.popToRoot();
            }
          );
        })
}