首页 > 文章列表 > 如何使用Python操作MySQL获取单个表的字段名和信息?

如何使用Python操作MySQL获取单个表的字段名和信息?

Python mysql
229 2023-04-28

获取单个表的字段名和信息的方法

import MySQLdb as mdb
import sys
#获取数据库的链接对象
con = mdb.connect('localhost', 'root', 'root', 'test')
with con:
#获取普通的查询 cursor
cur = con.cursor()
cur.execute("SELECT * FROM Writers")
rows = cur.fetchall()
#获取连接对象的描述信息
desc = cur.description
print 'cur.description:',desc
#打印表头,就是字段名字
print "%s %3s" % (desc[0][0], desc[1][0])
for row in rows:
#打印结果
print "%2s %3s" % row