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

您所在的位置:网站首页 mysql如何查看数据库名称 python MySQL 获取全部数据库(DATABASE)名、表(TABLE)名

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

2024-07-12 14:22| 来源: 网络整理| 查看: 265

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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3