首页 > 解决方案 > 从值为 NULL 的 DATETIME 获取结果时出现 python mariadb 连接器错误

问题描述

好吧,我想 mariadb 连接器有些不对劲...

我有一个看起来像这样的表格行:

ID 标题 类别 发布日期 截止日期 内容 文件 用户身份
6 测试 7 10 2021-04-01 21:50:21 内容 7 吸管f9cdaf.txt 16

问题是我有一个为 NULL 的 DATETIME 字段(date_due 日期在我的应用程序中不是强制性的)

似乎在将值传递给 execute() 方法之后, fetchall() 方法以不同的方式解析返回的数据。

第一个案例效果很好第二个返回“ValueError:第 0 年超出范围”

id=("6",)
statement1 = "select * from posts where id = 6"
statement2 = "select * from posts where id = ?"

db.cursor.execute(statement1)
result = db.cursor.fetchall()
for x in result:
     print(x)

db.cursor.execute(statement2, id)
result = db.cursor.fetchall()
for x in result:
     print(x)

我试图列出()连接器 - 结果相同

我试图更改值和表列属性 - 结果相同

我虽然是版本问题,所以我更新到最新的 python、mariadb 服务器和 python 连接器(在 ubuntu 20.04 上)——结果相同

结果:

6, 'test 7', 10, datetime.datetime(2021, 4, 1, 21, 50, 21), None, 'content 7', 'strawsf9cdaf.txt', 16)
ValueError: year 0 is out of range

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/project dir/main_test3.py", line 38, in <module>
    result = db.cursor.fetchall()
SystemError: <method 'fetchall' of 'mariadb.connection.cursor' objects> returned a result with an error set

Process finished with exit code 1

任何帮助将不胜感激

标签: pythonmysqlmariadb

解决方案


这是 MariaDB 连接器/Python 中的一个错误,最近由我自己修复。修复将在下一版本 (1.0.7) 中提供。

日期“0000-00-00”在 Python 中无效,因此必须转换为 None。

谢谢你的报告。


推荐阅读