74 lines
1.7 KiB
Python
74 lines
1.7 KiB
Python
|
|
import pymysql
|
||
|
|
import time
|
||
|
|
|
||
|
|
config = {
|
||
|
|
'host': 'localhost',
|
||
|
|
'port': 3306,
|
||
|
|
'user': 'root',
|
||
|
|
'password': 'rootx77',
|
||
|
|
'charset': 'utf8mb4'
|
||
|
|
}
|
||
|
|
|
||
|
|
print("连接数据库...")
|
||
|
|
conn = pymysql.connect(**config)
|
||
|
|
conn.autocommit = True
|
||
|
|
cursor = conn.cursor()
|
||
|
|
|
||
|
|
print("删除 ai 数据库...")
|
||
|
|
cursor.execute("DROP DATABASE IF EXISTS ai")
|
||
|
|
|
||
|
|
print("创建 ai 数据库...")
|
||
|
|
cursor.execute("CREATE DATABASE ai CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci")
|
||
|
|
|
||
|
|
print("切换到 ai 数据库...")
|
||
|
|
cursor.execute("USE ai")
|
||
|
|
|
||
|
|
print("读取 SQL 文件...")
|
||
|
|
with open('数据库.sql', 'r', encoding='utf8') as f:
|
||
|
|
sql = f.read()
|
||
|
|
|
||
|
|
print("开始导入...")
|
||
|
|
# 移除注释和空行
|
||
|
|
lines = []
|
||
|
|
for line in sql.split('\n'):
|
||
|
|
line = line.strip()
|
||
|
|
if line and not line.startswith('--') and not line.startswith('/*'):
|
||
|
|
lines.append(line)
|
||
|
|
|
||
|
|
sql_clean = ' '.join(lines)
|
||
|
|
|
||
|
|
# 按分号分割语句
|
||
|
|
statements = sql_clean.split(';')
|
||
|
|
|
||
|
|
count = 0
|
||
|
|
for stmt in statements:
|
||
|
|
stmt = stmt.strip()
|
||
|
|
if stmt:
|
||
|
|
try:
|
||
|
|
cursor.execute(stmt)
|
||
|
|
count += 1
|
||
|
|
if count % 50 == 0:
|
||
|
|
print(f"已执行 {count} 条语句...")
|
||
|
|
except Exception as e:
|
||
|
|
if 'Duplicate' not in str(e):
|
||
|
|
print(f"警告: {str(e)[:100]}")
|
||
|
|
|
||
|
|
print(f"\n总共执行 {count} 条语句")
|
||
|
|
|
||
|
|
# 验证
|
||
|
|
cursor.execute("SHOW TABLES")
|
||
|
|
tables = cursor.fetchall()
|
||
|
|
print(f"\n数据库中有 {len(tables)} 张表")
|
||
|
|
|
||
|
|
# 检查关键表
|
||
|
|
for table in ['nf_user', 'nf_lovers', 'nf_chat_message']:
|
||
|
|
cursor.execute(f"SHOW TABLES LIKE '{table}'")
|
||
|
|
if cursor.fetchone():
|
||
|
|
print(f"✅ {table}")
|
||
|
|
else:
|
||
|
|
print(f"❌ {table}")
|
||
|
|
|
||
|
|
cursor.close()
|
||
|
|
conn.close()
|
||
|
|
print("\n完成!")
|