taibai_shopping/create_test_user.py
2025-07-04 19:07:35 +08:00

73 lines
2.0 KiB
Python

import pymysql
import sys
from werkzeug.security import generate_password_hash
from datetime import datetime
# 数据库配置
config = {
'host': '27.124.22.104',
'user': 'taibai',
'password': 'taibaishopping',
'database': 'online_shopping',
'port': 3306,
'charset': 'utf8mb4'
}
def create_test_user():
try:
# 连接数据库
connection = pymysql.connect(**config)
print("✅ 数据库连接成功")
with connection.cursor() as cursor:
# 检查是否已存在测试用户
cursor.execute("SELECT id FROM users WHERE username = %s", ('testuser',))
existing_user = cursor.fetchone()
if existing_user:
print("✅ 测试用户已存在!")
print("用户名: testuser")
print("密码: 123456")
print("邮箱: test@example.com")
print("手机: 13800138000")
return
# 创建测试用户
password_hash = generate_password_hash('123456')
now = datetime.now()
sql = """
INSERT INTO users (username, phone, email, password_hash, nickname, status, created_at, updated_at)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
"""
cursor.execute(sql, (
'testuser',
'13800138000',
'test@example.com',
password_hash,
'测试用户',
1,
now,
now
))
connection.commit()
print("✅ 测试用户创建成功!")
print("用户名: testuser")
print("密码: 123456")
print("邮箱: test@example.com")
print("手机: 13800138000")
except Exception as e:
print(f"❌ 创建测试用户失败: {e}")
sys.exit(1)
finally:
if 'connection' in locals():
connection.close()
if __name__ == '__main__':
create_test_user()