简体   繁体   English

将元组转换为列表时,NoneType错误

[英]NoneType error when converting tuple to list

I am trying to select a row from a sqlite database. 我正在尝试从sqlite数据库中选择一行。 Make a number of changes then commit them back to the sqlite database as well as a mysql database. 进行一些更改,然后将其提交回sqlite数据库以及mysql数据库。

The way I am trying to do it is by converting the tuple retrieved by the SELECT query into a list to make the changes. 我尝试执行的方法是将SELECT查询检索到的tuple转换为list以进行更改。

key = 27361
c.execute("SELECT * FROM employees WHERE key = ?", (key,))
employeeTuple = c.fetchone()
employeeList = list(employeeTuple)

I am getting an error: 我收到一个错误:

TypeError: 'NoneType' object is not iterable

cursor.fetchone() returns None if there are no matching rows. 如果没有匹配的行, cursor.fetchone()返回None You have no rows where key = 27361 matches. 您没有key = 27361匹配的行。

You can test for that possibility using if employeeTuple or using or to shortcircuit and assign None to employeeList in that case: 您可以使用if employeeTuple或使用or短路来测试这种可能性,在这种情况下,将None分配给employeeList

# if employeeTuple is None employeeList will not be set at all
if employeeTuple:
    employeeList = list(employeeTuple)

or 要么

# if employeeTuple is None employeeList will be set to None
employeeList = employeeTuple or list(employeeTuple)

or 要么

# if employeeTuple is None employeeList will be set to the empty list
employeeList = list(employeeTuple or []) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM