import pymysql
host = '指定IP'
port = 指定端口
user = '用户名'
password = '密码'
database = '数据库名'
conn = pymysql.connect(
host=host,
port=port,
user=user,
password=password,
database=database
)
try:
cursor = conn.cursor()
new_schema = 'new_schema'
cursor.execute(f'CREATE SCHEMA {new_schema}')
old_schema = 'old_schema'
cursor.execute(f'SELECT table_name FROM information_schema.tables WHERE table_schema = "{old_schema}" AND table_type = "BASE TABLE"')
tables = cursor.fetchall()
for table in tables:
table_name = table[0]
new_table = f'{new_schema}.{table_name}'
cursor.execute(f'CREATE TABLE {new_table} LIKE {old_schema}.{table_name}')
cursor.execute(f'INSERT INTO {new_table} SELECT * FROM {old_schema}.{table_name}')
conn.commit()
finally:
cursor.close()
conn.close()