转载来至
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import MySQLdb as msd

db = msd.connect("192.168.1.51", "xsy", "xsy123", "demo")
# 使用cursor()方法获取操作游标  cursorclass = msd.cursors.DictCursor 加人这个就是获取键值对
cursor = db.cursor(cursorclass = msd.cursors.DictCursor)
try:
    cursor.execute("SELECT * FROM edai_usersystab LIMIT 100")

    results=cursor.fetchall()
    for row in results:
        print "id=%s,numbers=%s,username=%s,pass=%s,usertype=%s" % \
            (row['uid'], row['unumbers'], row['username'], row['userpass'], row['usertype'],)
except:
   print "Error: unable to fecth data"
# 关闭数据库连接
db.close()
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐