Instance Five:獲取單個表的字段名和信息的方法

#!/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()
        cur.execute("SELECT * FROM Writers")

        rows = cur.fetchall()

        desc = cur.description
        print "%s %3s" % (desc[0][0],desc[1][0])

        for row in rows:
            print "%2s %3s" % row
finally:
    con.close()

 Result:python

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