gpt4 book ai didi

angularjs - 如何使用 sqlite 数据库将外部服务器(JSON)的数据保存到 ionic 2 移动应用程序中

转载 作者:行者123 更新时间:2023-12-02 21:45:35 24 4
gpt4 key购买 nike

我所有的搜索都找到了有关 ionic 1 的帖子。在以下代码中,我可以在 ng*For 中使用 this.corpguys 并保存到本地存储。我想进一步改进我的应用程序并将其保存到 sqlite 表 corpguys 中。我不确定是否必须 JSON.stringify(data) 然后循环将此数据插入到数据库中。我只是想知道是否有人见过 ionic 2 的例子

import {Page, Storage, SqlStorage} from 'ionic/ionic';
import {Http} from 'angular2/http';
import 'rxjs/add/operator/map';

@Page ({
templateUrl: 'build/pages/remote/remote.html',
})

export class Remote {
constructor(http: Http) {
this.http = http;
//this.corpguys = null;
this.http.get('http://test.json')
.map(res => res.json())
.subscribe
(data => {
this.corpguys = data;

//Loop through corpguys and asign varible names

this.DataStorage = new Storage(SqlStorage);
this.DataStorage.query("INSERT into corpguys (name, position, phone, cell, sms, email, other) VALUES ('namevalue', 'positionvalue', 'phonevalue', 'cellvalue', 'smsvalue', 'emailvalue', 'othervalue')");
},
err => {
console.log("screwed up again!")
}
});
}

最佳答案

我决定另存为键值对。这是我的最终代码

import { Component } from '@angular/core';
import { Storage } from '@ionic/storage';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/timeout';
import { NavController, AlertController } from 'ionic-angular';

@Component({
selector: 'page-home',
templateUrl: 'home.html'
})

export class HomePage {
mainContact: any;

constructor(public navCtrl: NavController, public http: Http, public storage: Storage, public alertCtrl: AlertController) {
this.mainContact = null;
this.http.get('http://www.somejson.php')
.timeout(4000)
.map(res => res.json())
.subscribe(data => {
this.mainContact = data;
//console.log(data);
this.storage.set ("mainContact", JSON.stringify(data));
},
err => {
//console.log("http fail!");
this.connectionAlert();
this.storage.get("mainContact").then((value) => {
this.mainContact = JSON.parse(value));
}
);
}//end constructor

connectionAlert() {
let alert = this.alertCtrl.create({
title: 'Connection Error',
subTitle: 'Your network coverage is too weak for updates. Previously downloaded data will be used.',
buttons: ['CONTINUE']
});
alert.present();
}
}

关于angularjs - 如何使用 sqlite 数据库将外部服务器(JSON)的数据保存到 ionic 2 移动应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34818646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com