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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM