python-查詢數據庫中某一張表的列名

import pymysql
# 建立連接
connect_stat = pymysql.Connect(host='localhost', port=3306, user='root', passwd='123456', db='stats', charset='utf8')
cursor = connect_stat.cursor()
# 查詢sql
cursor.execute("select mobile_phone, create_time from t_tel_user_stat")
# 查詢sql中的列名集
description = cursor.description
# 取出列名集
column_list = [column[0] for column in description]
print('查詢結果中的列名:', column_list)
# 查詢表中所有列
cursor.execute("SHOW COLUMNS from t_tel_user_stat")
print('查詢表中全部列名:', [column[0] for column in cursor.fetchall()])

結果:

查詢結果中的列名: ['mobile_phone', 'create_time']
查詢表中全部列名: ['mobile_phone', 'tel_count', 'create_time', 'update_time']

查詢結果中的列名: ['mobile_phone', 'create_time'] 查詢表中全部列名: ['mobile_phone', 'tel_count', 'create_time', 'update_time']

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章