'Python and MySQL print results

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!



Solution 1:[1]

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]

Solution 2:[2]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ignacio Vazquez-Abrams
Solution 2 Jackob King