如果 Unique 约束失败,Room 的 OnConflict.replace 是否也有效?
Does Room's OnConflict.replace also works if Unique constraint fails?
所以,我有一个房间数据库,除了主键外,我还有一个唯一的索引列!
我想要的是,如果 -> 唯一约束失败 - 用新行替换旧行(也更改 PRIMARY KEY),OnConflictStrategy.REPLACE
会有帮助吗?
或者我应该在插入失败时删除以前的数据并添加新数据吗?
When a UNIQUE or PRIMARY KEY constraint violation occurs, the REPLACE algorithm deletes pre-existing rows that are causing the constraint violation prior to inserting or updating the current row and the command continues executing normally.
https://sqlite.org/lang_conflict.html
因此,如果 UNIQUE 约束失败,那么您想要的就会发生;以下演示了这一点:-
DROP TABLE IF EXISTS test;
CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, col1 INTEGER, col2 INTEGER, col3 INTEGER DEFAULT (strftime('%s-%f','now')));
CREATE UNIQUE INDEX IF NOT EXISTS idx_test_col1_col2 ON test (col1,col2);
INSERT INTO test (col1,col2) VALUES (1,1),(1,2),(2,1),(2,2);
SELECT * FROM test;
INSERT OR REPLACE INTO TEST (col1,col2) VALUES (2,1);
SELECT * FROM test;
DROP TABLE IF EXISTS test; -- Cleanup
结果是:-
- 唯一约束之前
然后:-
- 唯一冲突后。
这是一个更全面的测试,展示了其他排列(注意最后一个)
DROP TABLE IF EXISTS test;
CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, col1 INTEGER, col2 INTEGER, col3 INTEGER DEFAULT (strftime('%s-%f','now')));
CREATE UNIQUE INDEX IF NOT EXISTS idx_test_col1_col2 ON test (col1,col2);
INSERT INTO test VALUES (0,0,0,strftime('%s-%f','now'));
INSERT INTO test (col1,col2) VALUES (1,1),(1,2),(2,1),(2,2);
SELECT * FROM test; /* RESULT 1 INITIAL VALUES */
INSERT OR REPLACE INTO test (col1,col2) VALUES (2,1);
SELECT * FROM test; /* RESULT 2 UNIQUE conflict */
INSERT OR REPLACE INTO test (id,col1,col2) VALUES (1,7,7);
SELECT * FROM test; /* RESULT 3 PRIMARY KEY conflict */
INSERT OR REPLACE INTO test (id,col1,col2) VALUES (2,1,2);
SELECT * FROM test; /* RESULT 4 BOTH PRIMARY KEY and UNIQUE conflict on the same row (see timing) */
INSERT OR REPLACE INTO test (id,col1,col2) VALUES (4,7,7);
SELECT * FROM test; /* RESULTS 5 BOTH conflict BUT conflicts are for different rows */
DROP TABLE IF EXISTS test; -- Cleanup
这导致:-
- 最后插入 2 行被删除。由于 PRIMARY KEY 冲突,id 为 4 的行和由于 UNIQUE 冲突而具有 id 1 的行,并且添加了 1 行 (4,7,7)。
所以,我有一个房间数据库,除了主键外,我还有一个唯一的索引列!
我想要的是,如果 -> 唯一约束失败 - 用新行替换旧行(也更改 PRIMARY KEY),OnConflictStrategy.REPLACE
会有帮助吗?
或者我应该在插入失败时删除以前的数据并添加新数据吗?
When a UNIQUE or PRIMARY KEY constraint violation occurs, the REPLACE algorithm deletes pre-existing rows that are causing the constraint violation prior to inserting or updating the current row and the command continues executing normally.
https://sqlite.org/lang_conflict.html
因此,如果 UNIQUE 约束失败,那么您想要的就会发生;以下演示了这一点:-
DROP TABLE IF EXISTS test;
CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, col1 INTEGER, col2 INTEGER, col3 INTEGER DEFAULT (strftime('%s-%f','now')));
CREATE UNIQUE INDEX IF NOT EXISTS idx_test_col1_col2 ON test (col1,col2);
INSERT INTO test (col1,col2) VALUES (1,1),(1,2),(2,1),(2,2);
SELECT * FROM test;
INSERT OR REPLACE INTO TEST (col1,col2) VALUES (2,1);
SELECT * FROM test;
DROP TABLE IF EXISTS test; -- Cleanup
结果是:-
- 唯一约束之前
然后:-
- 唯一冲突后。
这是一个更全面的测试,展示了其他排列(注意最后一个)
DROP TABLE IF EXISTS test;
CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, col1 INTEGER, col2 INTEGER, col3 INTEGER DEFAULT (strftime('%s-%f','now')));
CREATE UNIQUE INDEX IF NOT EXISTS idx_test_col1_col2 ON test (col1,col2);
INSERT INTO test VALUES (0,0,0,strftime('%s-%f','now'));
INSERT INTO test (col1,col2) VALUES (1,1),(1,2),(2,1),(2,2);
SELECT * FROM test; /* RESULT 1 INITIAL VALUES */
INSERT OR REPLACE INTO test (col1,col2) VALUES (2,1);
SELECT * FROM test; /* RESULT 2 UNIQUE conflict */
INSERT OR REPLACE INTO test (id,col1,col2) VALUES (1,7,7);
SELECT * FROM test; /* RESULT 3 PRIMARY KEY conflict */
INSERT OR REPLACE INTO test (id,col1,col2) VALUES (2,1,2);
SELECT * FROM test; /* RESULT 4 BOTH PRIMARY KEY and UNIQUE conflict on the same row (see timing) */
INSERT OR REPLACE INTO test (id,col1,col2) VALUES (4,7,7);
SELECT * FROM test; /* RESULTS 5 BOTH conflict BUT conflicts are for different rows */
DROP TABLE IF EXISTS test; -- Cleanup
这导致:-
- 最后插入 2 行被删除。由于 PRIMARY KEY 冲突,id 为 4 的行和由于 UNIQUE 冲突而具有 id 1 的行,并且添加了 1 行 (4,7,7)。