pyqt5查询某个数据库下所有表格名称

import pymysql
#coursename=['语文', '化学', '物理']
def get_tables_from_db(database_name):
    tables = []
    # 连接参数配置
    config = {
        'host': 'localhost',
        'port': 3306,
        'user': 'root',
        'password': '数据库密码',
    }
    # 连接数据库
    con = pymysql.connect(**config)
    # 创建游标
    cursor = con.cursor()
    # 查询语句
    # 查询指定的数据库下有多少数据表

    #sql = 'select TABLE_NAME, table_type, engine from information_schema.tables where table_schema="语文"'                #这里是我“语文”数据库具体用法
    sql = 'select TABLE_NAME, table_type, engine from information_schema.tables where table_schema="' + database_name + '"'
    try:
        # 执行查询语句
        cursor.execute(sql)
        # 取得所有结果
        results = cursor.fetchall()
        # 打印数据表个数
        print(len(results))
        # 打印数据表名,数据表类型,及存储引擎类型
        print("table_name", "table_type", "engine")
        for row in results:
            name = row[0]
            type = row[1]
            engine = row[2]
            tables.append(name)
            print(name, type, engine)
    except Exception as e:
        raise e
    finally:
        con.close()
    return tables

#tables = get_tables_from_db(coursename[0])
#print(tables)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值