willtree8
willtree8

Reputation: 3

python - cannot use sqlite data

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.

Upvotes: 0

Views: 50

Answers (1)

forpas
forpas

Reputation: 164064

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.

Upvotes: 1

Related Questions