商城首页欢迎来到中国正版软件门户

您的位置:首页 >Python操作MySQL:获取单个表的字段名和信息

Python操作MySQL:获取单个表的字段名和信息

  发布于2023-05-01 阅读(0)

扫一扫,手机访问

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

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
本文转载于:https://www.yisu.com/zixun/658038.html 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注