1

我有一张桌子:

CREATE TABLE IF NOT EXISTS city_recent
            (_id INTEGER PRIMARY KEY AUTOINCREMENT,
            city_id INTEGER NOT NULL,
            language BOOL NOT NULL,
            type BOOL NOT NULL,
            FOREIGN KEY (city_id) REFERENCES city(_id),
            UNIQUE(city_id, type) ON CONFLICT IGNORE)

但独特的不起作用:

截屏

4

1 回答 1

3

我已经测试了您的代码,它按预期工作(测试如下所示)。最有可能发生的事情是表是在没有UNIQUE约束的情况下预先创建的。尝试删除IF NOT EXISTS以确认。

>>> import sqlite3
>>> con = sqlite3.connect(':memory:')
>>> con.execute('''CREATE TABLE IF NOT EXISTS city_recent
...             (_id INTEGER PRIMARY KEY AUTOINCREMENT,
...             city_id INTEGER NOT NULL,
...             language BOOL NOT NULL,
...             type BOOL NOT NULL,
...             FOREIGN KEY (city_id) REFERENCES city(_id),
...             UNIQUE(city_id, type) ON CONFLICT IGNORE);''')
<sqlite3.Cursor object at 0x01298FA0>
>>> con.execute('insert into city_recent(city_id,language,type) values (0,0,1);')
<sqlite3.Cursor object at 0x0129F120>
>>> con.execute('insert into city_recent(city_id,language,type) values (0,0,1);')
<sqlite3.Cursor object at 0x01298FA0>
>>> con.execute('select * from city_recent').fetchall()
[(1, 0, 0, 1)] # -> note that there is only one row in the table
于 2013-01-25T17:45:31.440 回答