Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2023-05-26 18:36:11
forward
1780 people have browsed it

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
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template