37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
"""导入 SQL 文件"""
|
|
import pymysql
|
|
|
|
try:
|
|
conn = pymysql.connect(
|
|
host='127.0.0.1',
|
|
port=3306,
|
|
user='root',
|
|
password='root',
|
|
database='lover',
|
|
charset='utf8mb4'
|
|
)
|
|
print("✓ 连接数据库成功")
|
|
|
|
with open('xunifriend.sql', 'r', encoding='utf8') as f:
|
|
sql_content = f.read()
|
|
|
|
cursor = conn.cursor()
|
|
# 分割并执行 SQL 语句
|
|
statements = sql_content.split(';')
|
|
for i, statement in enumerate(statements):
|
|
statement = statement.strip()
|
|
if statement:
|
|
try:
|
|
cursor.execute(statement)
|
|
if (i + 1) % 100 == 0:
|
|
print(f"已执行 {i + 1} 条语句...")
|
|
except Exception as e:
|
|
print(f"执行语句失败: {str(e)[:100]}")
|
|
|
|
conn.commit()
|
|
print(f"✓ SQL 导入完成,共执行 {len([s for s in statements if s.strip()])} 条语句")
|
|
cursor.close()
|
|
conn.close()
|
|
except Exception as e:
|
|
print(f"✗ 导入失败: {e}")
|