我正在开发一个ionic2项目,用户必须先登录才能访问系统。 当他们登录并成功时,我将其用户名发布到不同的API以获取他们所做的所有条目的列表,之后我想将所有返回的条目插入到sql lite db中,但我发现很难做到< / p>
userlogin(){
let loader = this.LoadingController.create({
content: 'Please Wait'
});
loader.present().then(()=>{
this.http.post("http://localhost/app/login.php", { 'username': this.username, 'password': this.password }).map(res => res.json()) .subscribe(data => {
if(data.message!="Incorrect Username or Password"){
this.http.post("http://localhost/app/entries.php", { 'username': this.username}).map(res => res.json()) .subscribe(data => {
console.log(JSON.stringify(data));
this.sqlite.create({
name: 'entries.db',
location: 'default'
})
.then((db: SQLiteObject) => {
//data insert section
db.executeSql('INSERT INTO entries_table(entry) VALUES(?)',
[this.data.entry]).then((data)=> {
}, (error) => {
});
})
});
}else{
if(data.message=="Incorrect Username or Password"){
let alert = this.alertCtrl.create({
title: 'Error!',
subTitle: 'Wrong Username or Password',
buttons: ['Try Again']
});
alert.present();
}
}
loader.dismiss();
});
},error=>{
let alert = this.alertCtrl.create({
title: 'Error!',
subTitle: 'Please check your Internet Connectivity',
buttons: ['Try Again']
});
alert.present();
})
}
登录工作正常,但问题是一次将API返回的多个数据插入到sql lite db
答案 0 :(得分:1)
使用离子存储并配置驱动程序以使用sqlite。那很简单
import { Storage } from '@ionic/storage';
export class MyApp {
constructor(private storage: Storage) { }
...
// set a key/value
storage.set('entries', returnedEntryObject);
// Or to get a key/value pair
storage.get('entires')
.then((returnedEntryObject) => {
console.log('The user entries are', returnedEtnryObject);
});
}
您可以在此link上查看详细信息。