0

I'm having an issue where I can print subscripted data from sqlite but cannot return it or assign it to a variable. Using python3 with sqlite in Windows 10.

def dbFindQuestion():
question = 'red'
sql = "SELECT * FROM words WHERE English=? OR German=?"
cursor.execute(sql, [(question), (question)])
print (cursor.fetchone()[0]) #this works
return (cursor.fetchone()[0]) #this doesn't
abc = (cursor.fetchone()[0]) # this doesn't

Printing the first column from the words table prints 'red' which is correct However, when I return it or set it as a variable it gives me the error:

TypeError: 'NoneType' object is not subscriptable

Why is this and how can I fix it? is it just me being stupid or is there a specific way of doing it?

Thanks in advance.

1
  • If your query only returns one row, the second time you call .fetchone() will return None, so cursor.fetchone()[0] will only work once. Commented Oct 29, 2018 at 19:48

1 Answer 1

1

Use this:

result = cursor.fetchone()[0]
print (result)
return (result)

The 1st fetchone() fetched only the 1 row you need and then threw it away, so the 2nd fetchone() you used did not fetch anything.

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.