Instance Four:使用字典cursor取得結果集(可使用表字段名字訪問值)

#!/usr/bin/env python
#--*-- coding:utf-8 --*--
__author__ = 'Kevin'

import MySQLdb as mdb
import sys

con = mdb.connect('localhost','root','redhat','test')

try:
    with con:
        cur = con.cursor(mdb.cursors.DictCursor)
        cur.execute("SELECT * FROM Writers")

        rows = cur.fetchall()

        for row in rows:
            print "%s %s" % (row["Id"],row["Name"])
finally:
    con.close()

 

Result:python

1 Jack London
2 Tik Tok
3 Harry Potter
相關文章
相關標籤/搜索