Skip to content Skip to sidebar Skip to footer

Python Sqlite3 Fetch Raw?

I have a problem with this code : cur.execute('SELECT Balance FROM accounts') print(cur.fetchone()) That outputs: (0,) instead of what I want 0. Can anyone help to fix the error?

Solution 1:

fetchone() would return you a single table row which may contain multiple columns. In your case it is a single column value returned in a tuple. Just get it by index:

data = cur.fetchone()
print(data[0])

Solution 2:

It's possible there would be more than one value in your query, so it always returns a tuple (you wouldn't want an interface which changes depending upon the data you pass it would you?).

You can unpack the tuple:

value, = cur.fetchone()

See the last paragraph of the documentation on tuples and sequences for information about sequence unpacking

Post a Comment for "Python Sqlite3 Fetch Raw?"