我需要将请求中的一些 JSON 格式的数据插入到我的应用程序中的 SQLite 数据库中。数据库正在工作并且表存在,但我不知道如何将此函数插入到表中。以下是我到目前为止的代码。为什么它不做插入?
synchronizeData() {
let db = new SQLite();
let loader = this.loadingCtrl.create({
content: "Sincronizando..."
});
loader.present();
this.account.allAccounts()
.subscribe(data =>
{
this.list = [];
//if (data.rows.length > 0) {
console.log(data.length);
for (var i = 0; i < data.length; i++) {
this.AccountId = data.rows.item(i).Id;
this.AccountIdentification = data.rows.item(i).Identification;
this.AccountActive = data.rows.item(i).Active;
this.AccountEditionUserId = data.rows.item(i).EditionUserId;
this.AccountEditionDateTime = data.rows.item(i).EditionDateTime;
this.AccountAccountTypeId = data.AccountType.rows.item(i).Id;
this.AccountComplement = data.rows.item(i).Complement;
this.AccountPriceListId = data.rows.item(i).PriceListId;
this.AccountColor = data.rows.item(i).Color;
this.AccountReferenceKey = data.rows.item(i).ReferenceKey;
if (data.rows.item(i).Active == 'true') { this.AccountActive = 1 } else { this.AccountActive = 0 }
//this.results.push({ name: data.rows.item(i).name });
this.db.executeSql("INSERT INTO Accounts" +
"(Id, SId, Identification, Active, EditionUserId, EditionDateTime, AccountTypeId, Complement, PriceListId, Color, ReferenceKey)" +
"VALUES(" + this.AccountId + ", 0 ," + this.AccountIdentification + " , " + this.AccountActive + " , " + this.AccountEditionUserId + "," +
this.AccountEditionDateTime + ", " + this.AccountAccountTypeId + "," + this.AccountComplement + "," + this.AccountPriceListId + "," +
this.AccountColor + "," + this.AccountReferenceKey +
")", []).then((data) => {
console.log("insert", data);
}, (error) => {
console.log("ERROR: " + JSON.stringify(error));
},
() => {
loader.dismiss();
})
}
//}
});
我没有看到你打开那个数据库
synchronizeData() {
let db = new SQLite();
db.openDatabase({
name: "data.db",
location: "default"
}).then(() => {
let loader = this.loadingCtrl.create({
content: "Sincronizando..."
});
loader.present();
this.account.allAccounts()
.subscribe(data =>
{
if(data){
this.list = [];
/* the rest of your code */
} else {
console.log("oops! data is undefined?!");
}
}
}
}
解决方案
insertIntoTable(query: any) {
this.database.openDatabase({
name: this.nameDb,
location: this.locationDb
}).then(() => {
this.database.transaction((tx) => {
this.database.executeSql(query, {}).then((data) => {
}, (error) => {
console.error("Unable to execute sql", error);
}), (error) => {
console.error("Unable to open database", error);
}
})
});
}
这是一个解决方案
synchronizeData() {
this.querys.selectAllTablesExists;
let loader = this.loadingCtrl.create({
content: "Sincronizando..."
});
loader.present();
this.AccountService.allAccounts()
.subscribe(data => {
this.list = [];
if (data.length > 0) {
for (var i = 0; i < data.length; i++) {
this.AccountId = data[i].Id;
this.AccountIdentification = data[i].Identification;
this.AccountEditionUserId = data[i].EditionUserId;
this.AccountEditionDateTime = data[i].EditionDateTime;
this.AccountAccountTypeId = data[i].AccountType.Id;
this.AccountComplement = data[i].Complement;
this.AccountPriceListId = data[i].PriceListId;
this.AccountColor = data[i].Color;
this.AccountReferenceKey = data[i].ReferenceKey;
if (data[i].Active == true) { this.AccountActive = 1 } else { this.AccountActive = 0 }
this.querys.insertIntoTable("insert or replace into Account (Id, SId, Identification, Active, EditionUserId, EditionDateTime, AccountTypeId, Complement, PriceListId, Color, ReferenceKey) VALUES('" + this.AccountId + "', '0' ,'" + this.AccountIdentification + "' , '" + this.AccountActive + "' , '" + this.AccountEditionUserId + "','" + this.AccountEditionDateTime + "', '" + this.AccountAccountTypeId + "','" + this.AccountComplement + "','" + this.AccountPriceListId + "','" + this.AccountColor + "','" + this.AccountReferenceKey + "')")
}
loader.dismiss();
}
})
}