I am running a sql in python which returns multiple number of records (of the order of millions) and somewhere in between I am getting a data of NonType
which is causing me issues as :TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
. My sql is:
select col1,col2,col3,col4,col5,col6,col7 from table1 where ....
This sql generates multiple records and I want to find out which record/column has that NoneType
data. How can I do that?
答案 0 :(得分:1)
如果要在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 ...)
答案 1 :(得分:0)
None is returned by the mysql adapter whenever a cell in the database contains a NULL. If you want to check the record's column for None you can do it with something like
if foo is None:
do_something()
答案 2 :(得分:0)
我知道这是一个老问题,但作为文档,这里有一个例子
用空字符串 None
替换 ""
我在使用包含 Null 值的 sqlite 数据库时遇到了类似的问题。
non_null = lambda x: "" if x is None else x
list = []
for row in cursor.execute("SELECT * FROM Table"):
# replace None
ls = map(non_null,row)
# create a new tuple because tuples are read-only
row1 = tuple(ls)
list.append(row1)
print(list)