11

I'm trying to teach myself Python but I've hit a brick wall. I need to get a field from MySQL however when I retrieve the data from the database it comes out odd. That's the code below I use.

cursor1 = db.cursor()
cursor1.execute("select djname from jerryins_djleaderboard.leaderboard where djname = %s", dj)
result = cursor1.fetchall()
print result

It prints out like this:

(('cutecrazygirl88\r\n',)

However I want it to come out as cutecrazygirl88 as it is in the database. Any help would be appreciated. Thank you in advance!

2
  • 3
    Except it's not "cutecrazygirl88" in the database. Commented Sep 3, 2012 at 0:03
  • also you might want to store just your data in the db without anyformatting your value has a newline with it. You can remove these for insert using docs.python.org/library/stdtypes.html#str.strip python strip method Commented Sep 3, 2012 at 0:08

2 Answers 2

23

fetchall() returns all fields and all rows in the cursor. You will need to iterate over the rows and access the fields in order to get at the data.

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

1 Comment

Silly oversight by me. Thank you for your help :)
0

To print everything the table contains with beautifultable:

from beautifultable import BeautifulTable
def function_name(result):
     table=BeautifulTable()
     table.column_headers["DJ Name"]
     for row in result:
          table.append_row(row)
     print(table)

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.