4

I am trying to print a tuple in python from a mysql query. An example query is as follows:

query = ("show databases;")

cursor.execute(query)

for row in cursor.fetchall():

       print (row)

The output is as follows:

('database1',)
('database2',)
('database3',)

How can I remove the characters (parenthesis and single quote) from tuple so that my output looks like this:

database1
database2
database3

3 Answers 3

7

The result from fetchall() is a list of tuples and not a list of strings.

print row[0]
Sign up to request clarification or add additional context in comments.

1 Comment

Then you could iterate through each tuple.
0

fetchall() returns tuple of tuples.
e.g. ((field1 of row1, field2 of row1, field3 of row1), (field1 of row2, field2 of row2, field3 of row2))

data=fetchall()
you can access first row as data[0]
you can access 2nd row as data[2]
you can access 1st filed of first row as data[0][0]
you can access 2nd filed of first row as data[0][1]
you can access 3rd filed of 2nd row as data[1][2]

Comments

-2
query = ("show databases;")

cursor.execute(query)

result = cursor.fetchall()

for row  in result :

    print ("".join(row  ))

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.