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)
pyqt5查询某个数据库下所有表格名称
于 2022-08-26 03:34:04 首次发布