gpt4 book ai didi

sqlite - IBM Worklight API Cordova存储-SQL错误:由于约束失败而无法执行语句(19个约束失败)

转载 作者:行者123 更新时间:2023-12-03 18:36:55 25 4
gpt4 key购买 nike

我在sqlite中的insert语句有问题。我有一个应用程序,它使用SQL适配器从远程数据库(localhost)获取数据,并将其存储到设备中。我在设备中创建了一个与“远程”数据库相同的模式的数据库。我想将所有记录从远程数据库复制到设备数据库,但是当我使用api cordova s​​torare运行insert语句时,由于约束失败(19约束失败),我得到的错误是无法执行语句。它很奇怪。

这是在设备上创建数据库的功能

function createLocalDb(size){

function createDB(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS canti (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"titolo VARCHAR(50) NOT NULL, " +
"autore VARCHAR(50) NOT NULL, " +
"id_categoria VARCHAR(50), " +
"testo TEXT," +
"UNIQUE(titolo,autore))");
tx.executeSql("CREATE TABLE IF NOT EXISTS categorie (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"titolo VARCHAR(50) NOT NULL UNIQUE, " +
"attiva INTEGER(1) DEFAULT '0')");

}

function errorCB(err) {
WL.Logger.debug("Error processing SQL on createLocalDb: " + err.message);
}

function successCB() {
WL.Logger.debug("Database created correctly");

WL.Client.invokeProcedure({
adapter : 'DbConnect',
procedure : 'getCanti',
parameters: []
}, {
onSuccess : success,
onFailure : failure
});

function success(result){
WL.Logger.debug("Success on invoking getCanti procedure");
populateCanti(result);
}
function failure(result){
WL.Logger.debug("Failure on invoking getCanti procedure");
}

WL.Client.invokeProcedure({
adapter : 'DbConnect',
procedure : 'getCategorie',
parameters: []
}, {
onSuccess : success2,
onFailure : failure2
});

function success2(result){
WL.Logger.debug("Success on invoking getCategorie procedure");
populateCategorie(result);
}
function failure2(result){
WL.Logger.debug("Failure on invoking getCategorie procedure");
}

}

if(size>0){
var db = window.openDatabase("db_canti", "1.0", "db_canti", size);
db.transaction(createDB, errorCB, successCB);
}
else{
WL.Logger.debug("Cannot create database with size 0");
}
}


这里是填充表的两个函数:

function populateCanti(item){

var db = window.openDatabase("db_canti", "1.0", "db_canti", size);
db.transaction(populateDB, errorCB, successCB);

function populateDB(tx){
for(var i=0;i<item.invocationResult.resultSet.length;i++){
WL.Logger.debug(i+")Sto inserendo " + item.invocationResult.resultSet[i].titolo + "," + item.invocationResult.resultSet[i].autore + "," + item.invocationResult.resultSet[i].id_categoria);
tx.executeSql("INSERT INTO canti(titolo,autore,id_categoria,testo) " +
"VALUES('"+item.invocationResult.resultSet[i].titolo+"','" +
+item.invocationResult.resultSet[i].autore+"','" +
+item.invocationResult.resultSet[i].id_categoria+"','" +
+item.invocationResult.resultSet[i].testo+"')");
}
}

function successCB(err) {
WL.Logger.debug("Table 'canti' OK");
checkLocalDb();
}

function errorCB(err) {
WL.Logger.debug("Error processing SQL on populateCanti: " + err.message);
}

}

function populateCategorie(item){

var db = window.openDatabase("db_canti", "1.0", "db_canti", size);
db.transaction(populateDB, errorCB, successCB);

function populateDB(tx){
for(var i=0;i<item.invocationResult.resultSet.length;i++){
tx.executeSql("INSERT INTO categorie(titolo,attiva) " +
"VALUES('"+item.invocationResult.resultSet[i].titolo+"','" +
+item.invocationResult.resultSet[i].attiva+"')");
}
}

function successCB(err) {
WL.Logger.debug("Table 'categorie' OK");
}

function errorCB(err) {
WL.Logger.debug("Error processing SQL on populateCategorie: " + err.message);
}

}


另外,表 categorie捕获了错误,但是插入显然起作用了,因为当我在浏览器上看到Web存储时,其中有19条记录。

最佳答案

如果记录在表中,则UNIQUE约束将阻止您插入重复项。

为防止重复项出错,请用INSERT OR IGNORE替换INSERT。

关于sqlite - IBM Worklight API Cordova存储-SQL错误:由于约束失败而无法执行语句(19个约束失败),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19359463/

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