gpt4 book ai didi

sqlite 多个唯一性不起作用

转载 作者:行者123 更新时间:2023-12-03 18:38:11 27 4
gpt4 key购买 nike

我有一张 table :

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)

但独特的不起作用:

screenshot

最佳答案

我已经测试了您的代码,它按预期工作(测试如下所示)。最有可能发生的事情是表是事先创建的而没有 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

关于sqlite 多个唯一性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14527312/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com