如何在 Python 中为 MySQL 设置空值
How to have a null value for MySQL in Python
这是我的 SQL 命令行版本。
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 122
Server version: 5.0.95 Source distribution
我有一个 table,并且从 Python 开始,我想将最后一列写为空。最终,特定行的最后一列将获得一个日期戳,我将用作不再处理该行的标志。
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| Action | char(1) | NO | | R | |
| EndpointId | int(11) | NO | MUL | NULL | |
| DeviceType | smallint(6) | NO | | NULL | |
| ChannelNumber | smallint(6) | NO | | 0 | |
| ActionDate | date | YES | | NULL | |
| InsertDate | date | YES | | NULL | |
+---------------+-------------+------+-----+---------+-------+
如何将空值放入插入查询字符串中?换句话说,我不知道如何在 Python 查询字符串中表示 NULL。
sql_cmd = \
"""insert into cs_remove
(
Action, EndpointId, DeviceType, ChannelNumber, ActionDate, InsertDate
)
VALUES
(
%s, %s, %s, %s, %s, %s
)
"""
print(base_date)
sql_data = ('R', ept_id, ept_type, ept_ch, "NULL", base_date);
该示例最终将 0000-00-00 放入日期字符串中。我想要空值。我可以从 mysql 命令行执行此操作,但不能从 Python 执行。任何帮助将不胜感激。
您应该在 python 中使用 None
值来表示您尝试插入的 NULL
mysql 值。像这样:
sql_data = ('R', ept_id, ept_type, ept_ch, base_date, None);
这是对另一个有此问题的 post 的引用:https://bytes.com/topic/python/answers/166025-python-mysql-insert-null
还有这个post:How can I insert NULL data into MySQL database with Python?
这是我的 SQL 命令行版本。
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 122
Server version: 5.0.95 Source distribution
我有一个 table,并且从 Python 开始,我想将最后一列写为空。最终,特定行的最后一列将获得一个日期戳,我将用作不再处理该行的标志。
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| Action | char(1) | NO | | R | |
| EndpointId | int(11) | NO | MUL | NULL | |
| DeviceType | smallint(6) | NO | | NULL | |
| ChannelNumber | smallint(6) | NO | | 0 | |
| ActionDate | date | YES | | NULL | |
| InsertDate | date | YES | | NULL | |
+---------------+-------------+------+-----+---------+-------+
如何将空值放入插入查询字符串中?换句话说,我不知道如何在 Python 查询字符串中表示 NULL。
sql_cmd = \
"""insert into cs_remove
(
Action, EndpointId, DeviceType, ChannelNumber, ActionDate, InsertDate
)
VALUES
(
%s, %s, %s, %s, %s, %s
)
"""
print(base_date)
sql_data = ('R', ept_id, ept_type, ept_ch, "NULL", base_date);
该示例最终将 0000-00-00 放入日期字符串中。我想要空值。我可以从 mysql 命令行执行此操作,但不能从 Python 执行。任何帮助将不胜感激。
您应该在 python 中使用 None
值来表示您尝试插入的 NULL
mysql 值。像这样:
sql_data = ('R', ept_id, ept_type, ept_ch, base_date, None);
这是对另一个有此问题的 post 的引用:https://bytes.com/topic/python/answers/166025-python-mysql-insert-null
还有这个post:How can I insert NULL data into MySQL database with Python?