我正在尝试从sqlite数据库中选择一行。进行一些更改,然后将它们提交回sqlite数据库以及mysql数据库。
我尝试这样做的方法是将tuple
查询检索到的SELECT
转换为list
进行更改。
key = 27361
c.execute("SELECT * FROM employees WHERE key = ?", (key,))
employeeTuple = c.fetchone()
employeeList = list(employeeTuple)
我收到错误:
TypeError: 'NoneType' object is not iterable
答案 0 :(得分:4)
cursor.fetchone()
会返回None
。您没有key = 27361
匹配的行。
您可以使用if employeeTuple
或使用or
短路来测试这种可能性,并在这种情况下将None
分配给employeeList
:
# if employeeTuple is None employeeList will not be set at all
if employeeTuple:
employeeList = list(employeeTuple)
或
# if employeeTuple is None employeeList will be set to None
employeeList = employeeTuple or list(employeeTuple)
或
# if employeeTuple is None employeeList will be set to the empty list
employeeList = list(employeeTuple or [])