【发布时间】:2017-10-10 00:07:00
【问题描述】:
请帮助我,我无法解决我在 ionic 2 sqlite 上的问题。在我的应用程序的第一次运行中,我成功创建了“玩家”表,我还可以在该表上插入和获取。但是当我尝试退出并重新打开我的应用程序时,它会提示我“plugin_not_installed”。
我正在关注 ionic frame work 的新文档。我在这个文档中观察到它没有一些教程总是提到的 db.openDatabase() 函数。
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
import { AlertController } from 'ionic-angular';
export class Players {
name:string; //ngmodel from html
players:new Array<Object>(); //used to list all players
constructor(private sqlite: SQLite,public alertCtrl: AlertController) {
this.initializeDatabase(); //create table if not exists
}
ionViewDidLoad(){
this.fetch();
}
initializeDatabase(){
this.sqlite.create({
name: 'data.db',
location: 'default'
})
.then((db: SQLiteObject) => {
db.executeSql('create table if not exists(playerId integer primary key,fullname varchar(50));', {})
.then(() => console.log('Executed SQL'))
.catch(e => this.showAlert( JSON.stringify(e)));
})
.catch(e => this.showAlert( JSON.stringify(e)));
}
fetch(){
let sql="select fullname from players";
this.sqlite.create({
name: 'data.db',
location: 'default'
}).then((db: SQLiteObject) => {
db.executeSql(sql, {})
.then((data) => {
this.players=[];
for(let index=0;index,data.rows.length;index++){
this.players.push({fullname:data.rows.item(index).fullname});
}
})
.catch(e => this.showAlert( JSON.stringify(e)));
})
.catch(e => this.showAlert( JSON.stringify(e)));
}
create(){
let name=this.name;
let sql="INSERT INTO players (playerId,fullname) values (?,?)";
this.sqlite.create({
name: 'data.db',
location: 'default'
}).then((db: SQLiteObject) => {
db.executeSql(sql, [null,name])
.then(() => console.log('successfully created'))
.catch(e => this.showAlert( JSON.stringify(e)));
})
.catch(e => this.showAlert( JSON.stringify(e)));
}
showAlert(msg:string) {
let alert = this.alertCtrl.create({
title: 'Info',
subTitle:msg,
buttons: ['OK']
});
alert.present();
}
}
【问题讨论】:
-
另外,当我多次尝试退出并重新打开我的应用程序时,有时不会出现此错误。
-
可以添加代码吗? minimal reproducible example
-
嗨,@suraj 先生,谢谢您的回复,我该如何添加代码?评论框说太长了
-
使用edit按钮..和
{}里面的按钮添加代码sn-p -
这可能会有所帮助:stackoverflow.com/a/50270455/813951
标签: angularjs sqlite typescript ionic2