Python 和 MySQL 打印结果 [英] Python and MySQL print results

查看:25
本文介绍了Python 和 MySQL 打印结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自学 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

打印出来是这样的:

(('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!

推荐答案

fetchall() 返回游标中的所有字段和所有行.您需要遍历行并访问字段以获取数据.

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]

这篇关于Python 和 MySQL 打印结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆