SQL 零而不是 null
此函数:
for i in Selection:
cursor.execute(Query)
ydata[i] = [int(x[0]) for x in cursor.fetchall()]
raise:
ValueError: invalid literal for int(): NULL if a null value is found.
如何使我的查询返回零而不是空值,以便我可以解决此问题? (我正在绘制数据,因此我无法将“is not null”添加到我的选择语句中。
This function:
for i in Selection:
cursor.execute(Query)
ydata[i] = [int(x[0]) for x in cursor.fetchall()]
raises:
ValueError: invalid literal for int(): NULL if a null value is found.
How can I make my query return zeros instead of nulls so that I can fix this? (I am plotting the data, so I cannot add "is not null" to my select statement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在查询中使用 case 语句在到达 Python 之前修复此问题:
开始编辑
其他 SQL 变体的做法不同(取自其他答案):
或
结束编辑
或者像这样在列表组件中处理它(假设 NULL 是预定义的对象或常量):
或者创建一个客户转换函数:
You can fix this before it gets to Python with a case statement in your query:
begin edit
Other SQL variants do it differently (taken from other answers):
or
end edit
Or handle it in your list comp like so (assumes NULL is a predefined object or constant):
Or create a customer conversion function:
将
IFNULL(fieldname, 0) AS fieldname
放入查询的字段列表中。Put
IFNULL(fieldname, 0) AS fieldname
in the field list of the query.您可以使用 IFNULL
You'd use IFNULL
该错误没有意义; Python 没有“NULL”,它有“None”。
最干净的做法是使用 SQL 合并:
SELECT COALESCE(value, 0)
将 SQL NULL 转换为 0。如果您得到 None,并且您知道这些值是整数,那么您可以安全地说
int(x[0]或0)
。The error doesn't make sense; Python doesn't have "NULL", it has "None".
The cleanest thing to do is use SQL coalesce:
SELECT COALESCE(value, 0)
to convert SQL NULL to 0.If you're getting None, and you know the values are integers, then you can safely say
int(x[0] or 0)
.