如何处理 SQLite 中的 'UNIQUE constraint failed' 异常
How to handle 'UNIQUE constraint failed' exception in SQLite
我是 React Native 的新手。
在我的应用程序中,我使用 SQLite 数据库作为本地数据库,(主键是参考号,这对每条记录都是唯一的)。在某些情况下,服务器会 return 相同的记录几次,并希望避免向数据库中添加重复项。
我想处理“UNIQUE constraint failed”异常。我尝试了一些解决方案,但它们对我不起作用。那我怎么处理呢。请帮我解决这个问题。
我的代码如下
addNewJobs(jobs: any) {
return new Promise((resolve) => {
this.initDB().then((db) => {
db.transaction((tx) => {
tx.executeSql('INSERT INTO LI_New_Retail (' +
DBProperties.CONSTANT_TABLE_COLUMN_REFERENCE_NO + ', ' +
DBProperties.CONSTANT_TABLE_COLUMN_REQUEST_DETAILS + ', ' +
DBProperties.CONSTANT_TABLE_COLUMN_INSPECTION_HISTORY + ', ' +
DBProperties.CONSTANT_TABLE_COLUMN_LOCATION_CONFIRMATION + ', ' +
DBProperties.CONSTANT_TABLE_COLUMN_DATE_SCHEDULED + ', ' +
DBProperties.CONSTANT_TABLE_COLUMN_CHECKLIST + ', ' +
DBProperties.CONSTANT_TABLE_JSON_RESPONSE + ', ' +
DBProperties.CONSTANT_TABLE_COLUMN_UPLOADED_IMAGE_LIST
+ ') VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
[jobs.referenceNumber, JSON.stringify(jobs.requestDetailsDTO),
JSON.stringify(jobs.inspectionHistoriesDtos), null,
JSON.stringify(jobs.dateScheduled),
JSON.stringify(jobs.pharmacyLocationChecklistsDTOS),
JSON.stringify(jobs), null])
.then(([tx, results]) => {
resolve(results);
})
}).then((result) => {
this.closeDatabase(db);
}).catch((error) => {
console.log("An error occured during data insert to db -- " + JSON.stringify(error));
this.closeDatabase(db);
});
}).catch((error) => {
console.log("adding new inspection jobs function error occured !!! -- " + error);
});
});
}
INSERT OR IGNORE INTO table-name
是要走的路...
If a record doesn't duplicate an existing record, then MySQL inserts
it as usual. If the record is a duplicate, then the IGNORE keyword
tells MySQL to discard it silently without generating an error.
我是 React Native 的新手。
在我的应用程序中,我使用 SQLite 数据库作为本地数据库,(主键是参考号,这对每条记录都是唯一的)。在某些情况下,服务器会 return 相同的记录几次,并希望避免向数据库中添加重复项。
我想处理“UNIQUE constraint failed”异常。我尝试了一些解决方案,但它们对我不起作用。那我怎么处理呢。请帮我解决这个问题。
我的代码如下
addNewJobs(jobs: any) {
return new Promise((resolve) => {
this.initDB().then((db) => {
db.transaction((tx) => {
tx.executeSql('INSERT INTO LI_New_Retail (' +
DBProperties.CONSTANT_TABLE_COLUMN_REFERENCE_NO + ', ' +
DBProperties.CONSTANT_TABLE_COLUMN_REQUEST_DETAILS + ', ' +
DBProperties.CONSTANT_TABLE_COLUMN_INSPECTION_HISTORY + ', ' +
DBProperties.CONSTANT_TABLE_COLUMN_LOCATION_CONFIRMATION + ', ' +
DBProperties.CONSTANT_TABLE_COLUMN_DATE_SCHEDULED + ', ' +
DBProperties.CONSTANT_TABLE_COLUMN_CHECKLIST + ', ' +
DBProperties.CONSTANT_TABLE_JSON_RESPONSE + ', ' +
DBProperties.CONSTANT_TABLE_COLUMN_UPLOADED_IMAGE_LIST
+ ') VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
[jobs.referenceNumber, JSON.stringify(jobs.requestDetailsDTO),
JSON.stringify(jobs.inspectionHistoriesDtos), null,
JSON.stringify(jobs.dateScheduled),
JSON.stringify(jobs.pharmacyLocationChecklistsDTOS),
JSON.stringify(jobs), null])
.then(([tx, results]) => {
resolve(results);
})
}).then((result) => {
this.closeDatabase(db);
}).catch((error) => {
console.log("An error occured during data insert to db -- " + JSON.stringify(error));
this.closeDatabase(db);
});
}).catch((error) => {
console.log("adding new inspection jobs function error occured !!! -- " + error);
});
});
}
INSERT OR IGNORE INTO table-name
是要走的路...
If a record doesn't duplicate an existing record, then MySQL inserts it as usual. If the record is a duplicate, then the IGNORE keyword tells MySQL to discard it silently without generating an error.