[[email protected]_server ~]# cat aa.py
import MySQLdb
conn=MySQLdb.connect(host=‘ip‘,user=‘xxx‘,passwd=‘xxx‘,port=xxx,db=‘xxxx‘,charset=‘utf8‘)
cursor =conn.cursor()
sql2="select ip,username,password FROM xxx.xxx"
cursor.execute(sql2)
conn.commit()
row=cursor.fetchall()
for i in row:
print i
[[email protected]_server ~]# cat bb.py
import MySQLdb
conn=MySQLdb.connect(host=‘ip‘,user=‘xxx‘,passwd=‘xxx‘,port=xxx,db=‘xxxx‘,charset=‘utf8‘)
cursor =conn.cursor()
sql2="select ip,username,password FROM xxx.xxx"
cursor.execute(sql2)
conn.commit()
row=cursor.fetchone()
for i in row:
print i
[[email protected]_server ~]# python bb.py
192.168.X.X
XXX
XXX
[[email protected]_server ~]# python aa.py
(u‘192.168.X.X1‘, u‘XXX‘, u‘XXX‘)
(u‘192.168.X.X2‘, u‘XXX‘, u‘XXX‘)
(u‘192.168.X.X3‘, u‘XXX‘, u‘XXX‘)
原文:http://www.cnblogs.com/lihbeibei/p/4718675.html
博客展示了Python使用MySQLdb库连接MySQL数据库的代码示例。包含两个Python脚本,分别使用fetchall和fetchone方法获取数据库查询结果,并打印输出,体现了Python与MySQL在数据交互方面的应用。
445

被折叠的 条评论
为什么被折叠?



