python 用字典形式查询mysql数据库

python 用字典形式查询mysql数据库

  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. import sys
  4. reload(sys)
  5. sys.setdefaultencoding('utf-8')
  6. import MySQLdb
  7. import MySQLdb.cursors

  8. db = MySQLdb.connect("192.168.101.234", "root", "luca321", "monworks",cursorclass=MySQLdb.cursors.DictCursor)
  9. cursor = db.cursor()
  10. cursor.execute("select id,name from monworks")
  11. results = cursor.fetchall()
  12. for row in results:
  13. print row["id"]

猜你喜欢

转载自blog.csdn.net/peyte1/article/details/79137048