Home  >  Article  >  Database  >  How to use Python to obtain the field names and information of a single table using MySQL

How to use Python to obtain the field names and information of a single table using MySQL

WBOY
WBOYforward
2023-05-26 18:36:111735browse

Method to obtain field names and information of a single table

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

The above is the detailed content of How to use Python to obtain the field names and information of a single table using MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete