pymsql 查询某一数据库的所有数据表

来源:互联网 发布:mac版yy怎么录音 编辑:程序博客网 时间:2024/06/05 17:19

要求:使用 pymysql 连接数据库,查询某一指定数据库中的所有数据表

代码实现:

import pymysqldef get_tables_from_db():    tables = []    # 连接参数配置    config = {        'host': '主机地址',        'port': 3306,        'user': 'mysql用户名',        'password': '密码',    }    # 连接数据库    con = pymysql.connect(**config)    # 创建游标    cursor = con.cursor()    # 查询语句    # 查询指定的数据库下有多少数据表    sql = 'select TABLE_NAME, table_type, engine from information_schema.tables where table_schema="指定的数据库名"'    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 tablestables = get_tables_from_db()
阅读全文
0 0