简体   繁体   English

Python3 SQLite3-如果值为null则不插入

[英]Python3 Sqlite3 - Not insert if value is null

I'm using Python3.6 and sqlite3 我正在使用Python3.6和sqlite3

How can I insert values to the database only if they are not null? 仅当值不为null时才如何将值插入数据库? I have tested the database by inserting 200 duplicated rows just for sure that INSERT OR IGNORE works good and after that I get 200 rows all filled by the NULL. 我已经通过插入200条重复的行来测试数据库,只是为了确保INSERT或IGNORE正常工作,然后获得200条全部由NULL填充的行。

My INSERT code is: 我的INSERT代码是:

cursor.execute("""INSERT OR IGNORE INTO mytable (
    column1,
    column2,
    column3,
    column4,     /* INT, PRIMARY KEY, UNIQUE
    column5,
    column6) 
VALUES (?,?,?,?,?,?)""",
(
    value1,
    value2,
    value3,
    value4,
    value5,
    value6))
conn.commit()

How to fix that? 如何解决?

Now, when I'm writing this, I think it's maybe becouse my PRIMARY KEY column is not the first one? 现在,当我写这篇文章时,我想可能是因为我的PRIMARY KEY列不是第一个? Could it be true? 会是真的吗?

INSERT OR IGNORE is saying to do the insert BUT to ignore conflicts (don't do them). INSERT OR IGNORE表示要执行插入BUT来忽略冲突(不要这样做)。 A conflict occurs when a CONSTRAINT is breached/not obeyed. 违反/不遵守约束时会发生冲突。

It would appear that you have few CONSTRAINTS and at a guess only implicit CONSTRAINTS. 看来您几乎没有约束,而猜测只有隐性约束。 That is if you have column4 INTEGER PRIMARY KEY or the same with AUTOINCREMENT then the implicit CONSTRAINTS,2 of them, are UNIQUE NOT NULL. 也就是说,如果您具有column4 INTEGER PRIMARY KEY或具有AUTOINCREMENT的相同column4 INTEGER PRIMARY KEY ,则其中的2个隐式CONSTRAINTS为UNIQUE NOT NULL。 However such a column, an alias of the rowid , is special and specifying NULL results in the column being assigned an integer that is unique within the table (ie it uniquely identifies the row). 但是,此类列( rowid的别名)是特殊的,并且指定NULL会导致为该列分配一个在表内唯一的整数(即,它唯一地标识该行)。

PS The column position makes no difference. PS列位置没有区别。

So going back to the guess none of the columns have a specific CONSTRAINT coded so the ignore won't be triggered as there will be no CONFLICT as there are no CONSTRAINTS. 因此,回到猜测,这些列都没有编码为特定的CONSTRAINT,因此不会触发忽略,因为不会有冲突,因为没有约束。

What you should do is for every column that you don't want NULL to be a valid value is to change the column definition to include NOT NULL in the column definition (after the column name and the column type). 对于不希望NULL为有效值的每个列,您应该做的是更改列定义,以在列定义中(在列名和列类型之后)包括NOT NULL。

So instead of (as an example) of having :- 因此,代替(例如)具有:-

CREATE TABLE mytable (
    column1 TEXT,
    column2 TEXT,
    column3 TEXT,
    column4 INTEGER PRIMARY KEY,
    column5 TEXT,
    column6 TEXT
);

You would have :- 你将会拥有 :-

CREATE TABLE mytable (
    column1 TEXT NOT NULL,
    column2 TEXT NOT NULL,
    column3 TEXT NOT NULL,
    column4 INTEGER PRIMARY KEY,
    column5 TEXT NOT NULL,
    column6 TEXT NOT NULL
);

Thus using the above table along with the following SQL :- 因此,将上表与以下SQL结合使用:

-- NOTE column4 omitted from list of columns thus null
INSERT OR IGNORE INTO mytable (column1,column2,column3,column5,column6) VALUES
    (1,2,3,4,5),        -- Will be inserted as no conflict
    (6,7,null,null,10), -- conflict so ignored
    (11,12,13,14,15),  -- no conflict
    (null,17,18,19,20), -- conflict
    (21,22,23,24,25), -- no conflict
    (26,null,28,29,230) -- conflict
;

Only 3 rows will be inserted as per and no nulls even though attempts were made to insert nulls:- 即使试图插入空值,也只能按每行插入3行,并且不会有空值:-

在此处输入图片说明

Whilst if the table were defined according to the first CREATE SQL above, the result of the INSERT SQL would have been :- 如果表是根据上面的第一个CREATE SQL定义的,则INSERT SQL的结果将是:-

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM