gpt4 book ai didi

mysql - SQLite (Ionic 2) 行中的重复值

转载 作者:行者123 更新时间:2023-11-29 10:11:52 25 4
gpt4 key购买 nike

创建表的代码

  if (!this.isOpen) {
this.storage = new SQLite();
this.storage.create({ name: "form.db", location: "default" }).then((db: SQLiteObject) => {
this.db = db;
db.executeSql("CREATE TABLE IF NOT EXISTS formstable (formId VARCHAR PRIMARY KEY, clientId VARCHAR, formsdata VARCHAR)", []);
console.log("table created successfully");
this.isOpen = true;
}).catch((error) => {
console.log(error);
})
}

这是我用来向sql lite插入数据的函数

 Savedata(formId:string, clientId:string,formsdata:string){
return new Promise ((resolve, reject) => {
let sql = "INSERT OR REPLACE INTO formstable (formId, clientId,formsdata) VALUES (?,?,?)";
this.db.executeSql(sql, [formId, clientId,formsdata]).then((data) =>{
resolve(data);
console.log(data);
console.log("data inserted successfully")
}, (error) => {
reject(error);
});
});
}

从 sql lite 获取值的函数

 GetAlldata(){
return new Promise ((resolve, reject) => {
this.db.executeSql("SELECT * FROM formstable", []).then((data) => {
let arrayUsers = [];
if (data.rows.length > 0){
for (var i = 0; i < data.rows.length; i++) {
arrayUsers.push({
formId: data.rows.item(i).formId,
clientId: data.rows.item(i).clientId,
formsdata: JSON.parse(data.rows.item(i).formsdata)
});
}
}
resolve(arrayUsers);
}, (error) => {
reject(error);
})
})
}

表中我的值“formsdata”是一个JSON,它被转换为字符串并保存到数据库中,并在获取时解析为json。每次我调用函数“savadata( )"一次又一次插入三个值(formid、clientid、formsdata)。那么如何避免这种重复的值,我尝试了“替换”关键字,但没有成功

最佳答案

您必须将“formId”设为主键。在表架构中,将必要字段更改为主键,以便替换有效。

关于mysql - SQLite (Ionic 2) 行中的重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50828655/

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