sqlite insert或ignore始终在空字段上插入

2024-06-02 04:58:57 发布

您现在位置:Python中文网/ 问答频道 /正文

当一个字段为空时,尽管使用了一个主键insert or ignore,但我还是得到了多个插入。我还尝试添加一个唯一的索引,只是为了确定。我只希望在主键不等于任何现有行时进行新的插入。

根据sqlite.org/nulls.htmlnulls are distinct in a UNIQUE column,这些重复插入不应该发生。

我在做这个插入:

INSERT OR IGNORE INTO facts (
  created,
  inserted,
  author,
  kind,
  what,
  field,
  val
  ) VALUES (?, ?, ?, ?, ?, ?, ?)

有了这张桌子:

^{pr2}$

在多次执行相同的插入操作后得到这个结果:

sqlite> select * from facts where what='vot1dafjt95326qvs8kn' order by inserted;
2014-01-29T09:30:44.000000|2015-07-23T13:23:10.643060|test_user_id|TaskFact|vot1dafjt95326qvs8kn|created|2014-01-29T09:30:44.000000
2014-01-29T09:30:44.000000|2015-07-23T13:23:10.643152|test_user_id|TaskFact|vot1dafjt95326qvs8kn|alarm|
2014-01-29T09:30:44.000000|2015-07-23T13:23:10.643315|test_user_id|TaskFact|vot1dafjt95326qvs8kn|deleted|
2014-01-29T09:30:44.000000|2015-07-23T13:23:10.643380|test_user_id|TaskFact|vot1dafjt95326qvs8kn|description|
2014-01-29T09:30:44.000000|2015-07-23T13:23:10.643445|test_user_id|TaskFact|vot1dafjt95326qvs8kn|location|KG45
2014-01-29T09:30:44.000000|2015-07-23T13:23:10.643641|test_user_id|TaskFact|vot1dafjt95326qvs8kn|summary|semtid
2014-01-29T09:30:44.000000|2015-07-23T13:23:10.643780|test_user_id|TaskFact|vot1dafjt95326qvs8kn|when|TS('2014-03-14T07:00:00.000000', '2014-03-14T11:00:00.000000')
2014-01-29T09:30:44.000000|2015-07-23T13:23:35.559110|test_user_id|TaskFact|vot1dafjt95326qvs8kn|alarm|
2014-01-29T09:30:44.000000|2015-07-23T13:23:35.559273|test_user_id|TaskFact|vot1dafjt95326qvs8kn|deleted|
2014-01-29T09:30:44.000000|2015-07-23T13:24:12.548969|test_user_id|TaskFact|vot1dafjt95326qvs8kn|alarm|
2014-01-29T09:30:44.000000|2015-07-23T13:24:12.549186|test_user_id|TaskFact|vot1dafjt95326qvs8kn|deleted|
2014-01-29T09:30:44.000000|2015-07-23T13:25:39.638743|test_user_id|TaskFact|vot1dafjt95326qvs8kn|alarm|
2014-01-29T09:30:44.000000|2015-07-23T13:25:39.638906|test_user_id|TaskFact|vot1dafjt95326qvs8kn|deleted|

deleted和{}行被多次插入,但其他行只插入一次。

我正在使用sqlite3模块执行python3中的插入。


Tags: ortestidsqlitewhatinsertcreated主键
1条回答
网友
1楼 · 发布于 2024-06-02 04:58:57

您误解了“nulls is distinct in a UNIQUE column”。这意味着对于UNIQUE约束而言,NULL == NULL为false,因此可以插入多个{}。在

注意,从技术上讲,您的列是PRIMARY KEY,而不是{}。在SQL中,主键必须是NOT NULL;由于bug转换的特性,SQLite只支持NULL主键。在

相关问题 更多 >