带有SQL请求的重复键上的NS_ERROR_FAILURE

时间:2014-05-21 22:13:58

标签: sqlite

我试图在我的数据库上运行此请求:

INSERT INTO scnScenarios (id,version,description,author,created_at,updated_at,tailNumber,flightLegCounter,name ) 
VALUES ('98','','','','2014-05-21 22:04:05','','9001','12','bolo') ON DUPLICATE KEY UPDATE ID=last_insert_rowid() , description='',author='';

返回此错误:

SQLiteManager: Likely SQL syntax error: INSERT INTO scnScenarios (id,version,description,author,created_at,updated_at,tailNumber,flightLegCounter,name ) 
VALUES ('98','','','','2014-05-21 22:04:05','','9001','12','bolo') ON DUPLICATE KEY UPDATE ID=last_insert_rowid() , description='',author=''; [ near "ON": syntax error ]
Exception Name: NS_ERROR_FAILURE
Exception Message: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [mozIStorageConnection.createStatement]

1 个答案:

答案 0 :(得分:0)

SQLite的INSERT statement没有ON DUPLICATE KEY子句。

处理此问题的最简单方法是使用多个语句:首先尝试UPDATE,如果记录不存在(如果受影响的记录数为零),则执行INSERT。

另见SQLite - UPSERT not INSERT or REPLACE