Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python and MySQL print results

Tags:

python

mysql

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!

like image 622
Christian Avatar asked Sep 03 '12 00:09

Christian


1 Answers

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]
like image 63
Ignacio Vazquez-Abrams Avatar answered Nov 12 '22 03:11

Ignacio Vazquez-Abrams