用 pymysql 列印 MySQL/MariaDB 的所有庫名、表名和欄位名

來源:互聯網
上載者:User

標籤:ati   als   databases   HERE   auth   proc   where   bar   pwd   

【環境】

  • Python 版本:3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]

  • PyMySQL 版本:0.8.1


【代碼】

#encoding: utf-8#author: walker#date: 2018-07-26#summary: 列印 MySQL/MariaDB 裡面的所有庫名、表名和欄位名import pymysqlimport pprint DBHost = r'127.0.0.1'DBPort = 3306DBUser = 'root'DBPwd = 'password'# 忽略掉系統庫IgnoreDB = {'information_schema', 'mysql', 'performance_schema', 'sys'}     # 處理一個資料庫def ProcOneDB(dbName):    print('************ use %s ************' % dbName)    connDB = pymysql.connect(host=DBHost,                                port=DBPort,                                user=DBUser,                                passwd=DBPwd,                                db=dbName,                                charset='utf8mb4')    cur = connDB.cursor()         sql = 'show tables;'    cur.execute(sql)    rowList = cur.fetchall()    tableList = list()    for row in rowList:        tableList.append(row[0])    print('tableList(%d):\n%s\n' % (len(tableList), pprint.pformat(tableList, indent=4)))         # 處理每個表    for tabName in tableList:        print('table %s ...' % tabName)        sql = "select column_name from information_schema.columns where table_schema='%s' and table_name='%s';"        sql = sql % (dbName, tabName)        cur.execute(sql)        rowList = cur.fetchall()        fieldList = list()        for row in rowList:            fieldList.append(row[0])        print('fieldList(%d):\n%s\n' % (len(fieldList), pprint.pformat(fieldList, indent=4)))         cur.close()    connDB.close()  # 處理所有資料庫def ProcAllDB():    connDB = pymysql.connect(host=DBHost,                                port=DBPort,                                user=DBUser,                                passwd=DBPwd,                                charset='utf8mb4')                                     cur = connDB.cursor()             sql = "show databases;"     print('input sql:' + sql)    cur.execute(sql)    rowList = cur.fetchall()    cur.close()    connDB.close()         dbList = list()    for row in rowList:        dbList.append(row[0])    print('dbList(%d):\n%s\n' % (len(dbList), pprint.pformat(dbList, indent=4)))         for dbName in dbList:        if dbName in IgnoreDB:            continue        ProcOneDB(dbName)                                                                                   if __name__ == '__main__':    ProcAllDB()


*** walker ***


用 pymysql 列印 MySQL/MariaDB 的所有庫名、表名和欄位名

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.