0

I am trying to select a row from a sqlite database. Make a number of changes then commit them back to the sqlite database as well as a mysql database.

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.

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
1
  • Straight from the docs "Fetches the next row of a query result set, returning a single sequence, or None when no more data is available" Commented Apr 2, 2015 at 9:51

1 Answer 1

4

cursor.fetchone() returns None if there are no matching rows. You have no rows where key = 27361 matches.

You can test for that possibility using if employeeTuple or using or to shortcircuit and assign None to employeeList in that case:

# 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 []) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.