30 lines
902 B
Python
30 lines
902 B
Python
|
|
"""添加测试用户"""
|
||
|
|
import os
|
||
|
|
import pymysql
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
SERVER_DIR = Path(__file__).parent.parent
|
||
|
|
env_file = SERVER_DIR / '.env'
|
||
|
|
if env_file.exists():
|
||
|
|
with open(env_file, 'r', encoding='utf-8') as f:
|
||
|
|
for line in f:
|
||
|
|
line = line.strip()
|
||
|
|
if line and not line.startswith('#') and '=' in line:
|
||
|
|
key, value = line.split('=', 1)
|
||
|
|
os.environ.setdefault(key.strip(), value.strip())
|
||
|
|
|
||
|
|
conn = pymysql.connect(
|
||
|
|
host=os.getenv('DB_HOST', 'localhost'),
|
||
|
|
port=int(os.getenv('DB_PORT', 3306)),
|
||
|
|
user=os.getenv('DB_USER', 'root'),
|
||
|
|
password=os.getenv('DB_PASSWORD', ''),
|
||
|
|
database='stardom_story',
|
||
|
|
charset='utf8mb4'
|
||
|
|
)
|
||
|
|
cur = conn.cursor()
|
||
|
|
cur.execute("INSERT IGNORE INTO users (id, openid, nickname) VALUES (1, 'test_user', '测试用户')")
|
||
|
|
conn.commit()
|
||
|
|
print('测试用户创建成功')
|
||
|
|
cur.close()
|
||
|
|
conn.close()
|