SQLite语法REPLACE + WHERE

时间:2015-11-08 09:21:40

标签: sqlite

我在以下代码中做错了什么?

CREATE TABLE IF NOT EXISTS chunks(x Integer(20) NOT NULL, y Integer(20) NOT NULL, 
            height Integer(20) NOT NULL, version Timestamp default (strftime('%s', CURRENT_TIME)), 
            datas Text NOT NULL);
REPLACE INTO chunks VALUES(0,0,0,NULL,'Bla') WHERE x IS 0 AND y IS 0 AND height IS 0;

1 个答案:

答案 0 :(得分:0)

REPLACE要求表对要搜索的列具有UNIQUE约束,并且如果找到旧行,则始终删除旧行。

更好的想法是明确检查该行是否存在:

cursor.execute("UPDATE chunks SET ... WHERE x = ? AND y = ? AND height = ?",
               [0, 0, 0])
if cursor.rowcount == 0:
    cursor.execute("INSERT INTO chunks ...")