python MySQL 获取全部数据库(DATABASE)名、表(TABLE)名

import MySQLdb

#connect   

try:

    conn = MySQLdb.connect(

        host = "localhost",

        user = "root",

        passwd = "root",

        #db = "dome"

        )

    cur = conn.cursor()

    #获取mysql中所有数据库

    cur.execute('SHOW DATABASES')

    print(cur.fetchall())

 

    #创建数据库

    cur.execute('create database if not exists dome')

    conn.select_db('dome')

    #创建表

    cur.execute('create table if not exists T_Person(id int,info varchar(20))')

    #获取dome数据库中所有表

    cur.execute('SHOW TABLES')

    print(cur.fetchall())

except MySQLdb.Error, e:

    print("Mysql Error %d: %s" % (e.args[0], e.args[1]))

原文地址:https://www.cnblogs.com/doudongchun/p/3694804.html