Print all library names, table names, and field names for mysql/mariadb with Pymysql

Source: Internet
Author: User
Tags pprint

Environment

    • Python version: 3.6.5 (V3.6.5:F59C0932B4, Mar 2018, 17:00:18) [MSC v.1900 bit (AMD64)]

    • Pymysql version: 0.8.1


Code

#encoding: utf-8#author: walker#date: 2018-07-26#summary:  Printing  MySQL/MariaDB  All library names, table names, and field names Import pymysqlimport pprint dbhost = r ' 127.0.0.1 ' dbport =  3306dbuser =  ' root ' dbpwd =  ' password ' #  ignore system library ignoredb = {' Information_ Schema ',  ' mysql ',  ' performance_schema ',  ' sys '}     #  processing a database 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:&nbsP;       tablelist.append (Row[0])     print (' TableList (% d): \n%s\n '  %  (Len (tablelist),  pprint.pformat (tablelist, indent=4)))           #  Process each table     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 ()   #  process all databases def  Procalldb ():     conndb = pymysql.connect (host=dbhost,                                  port=DBPort,                                  user=DBUser,                &nbsP;                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 =&NBsp;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 * * *


Print all library names, table names, and field names for mysql/mariadb with Pymysql

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.