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]))

posted on 2014-04-27 16:34  SpringStudio  阅读(10058)  评论(0编辑  收藏  举报

导航