gpt4 book ai didi

python - 如何检查sql是否在python中返回非类型数据

转载 作者:行者123 更新时间:2023-11-28 23:52:50 24 4
gpt4 key购买 nike

我在 python 中运行一个 sql,它返回多个记录(数以百万计),在两者之间的某个地方,我得到一个 NonType 的数据,这导致我出现问题:TypeError:+ 不支持的操作数类型:“NoneType”和“str”。我的 sql 是:

从 table1 中选择 col1,col2,col3,col4,col5,col6,col7,其中 ....

此 sql 生成多个记录,我想找出哪个记录​​/列具有该 NoneType 数据。我该怎么做?

最佳答案

如果你想在 Python 级别上解决它,你可以迭代结果并使用 any()检测一行中的任何 None 值:

for result in cursor.fetchall():
if any(field is None for field in result):
print(result)
break # break once we meet a row with None value

但是,为什么不在数据库层面解决呢?例如,查找任何列具有 NULL 值的所有行:

select 
col1,col2,col3,col4,col5,col6,col7
from
table1
where
(col1 IS NULL or col2 IS NULL or ...)

关于python - 如何检查sql是否在python中返回非类型数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32341878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com