Hey guys just a small question on this connection here. I wanted to print the output of the sql db using python which is currently successful but the output is not what i desired.
def query(conn):
x = conn.cursor()
x.execute ("SELECT Amount from Expenses")
for item in x.fetchall():
items = map(int,item)
print "The amount is %s" % (items)
output: The amount is [15000] --> which is the concern here. I want to get rid of the square brackets in the output. Please help me out on this
2 Answers 2
The response you are getting if for items which can be multiple and it return a list.
So you are getting [15000]
How can i get rid of square bracket.
simple items[0]
If you are expecting multiple value and want to get all just simple iterate over a loop.
for item in items:
print item
Comments
fetchall return you a list of tuples. You do not need map also. You can directly access the element using index.
Ex:
def query(conn):
x = conn.cursor()
x.execute ("SELECT Amount from Expenses")
for item in x.fetchall():
#items = int(item[0]) -->Use index to access the element.
print "The amount is %s" % (item[0])
2 Comments
L is just a representation for "Long".
items[0]?....