MySql Python get a table of all field names

When using python after export data in the database, often in addition to the insertion of data, as well as tables and other information fields to be exported, access to the information found in the two methods

The first: built-in mysql query table, this table contains the field information for each table, you can execute the following sql statement with pymysql

import pymysql
conn = pymysql.connect(host="127.0.0.1",user="root",password="123456",db="study",autocommit=True)
cur = conn.cursor()
sql = "select COLUMN_NAME from information_schema.COLUMNS where table_name = 'userinfo'"
cur.execute(sql)
for field in cur.fetchall():
    print(field[0])
cur.close()
conn.close()

The second: use built-in method to get pymysql

import pymysql
conn = pymysql.connect(host="127.0.0.1",user="root",password="123456",db="study",autocommit=True)
cur = conn.cursor()
sql = "select * from userinfo"
result = cur.execute(sql)
desc = cur.description
for field in desc:
    print(field[0])
cur.close()
conn.close()

Guess you like

Origin www.cnblogs.com/jruing/p/12458117.html