我对insert
sqlite 中的语句有疑问。我有一个应用程序,它使用 sql 适配器从远程数据库(localhost)获取数据并将它们存储到设备中。我已经在设备中创建了一个与“远程”数据库相同架构的数据库。我想将所有记录从远程数据库复制到设备数据库,但是当我insert
使用 api cordova storare 运行语句时,由于约束失败(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 条记录。