提示词专家
This commit is contained in:
73
create_default_user.py
Normal file
73
create_default_user.py
Normal file
@@ -0,0 +1,73 @@
|
||||
import pymysql
|
||||
from datetime import datetime
|
||||
import hashlib
|
||||
import random
|
||||
import string
|
||||
|
||||
def create_default_user():
|
||||
"""创建默认用户"""
|
||||
try:
|
||||
# 连接MySQL数据库
|
||||
conn = pymysql.connect(
|
||||
host='localhost',
|
||||
user='root',
|
||||
password='123456',
|
||||
database='food_db',
|
||||
charset='utf8mb4'
|
||||
)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# 检查是否已存在默认用户
|
||||
cursor.execute("SELECT uid FROM user WHERE login_name = 'admin'")
|
||||
if cursor.fetchone():
|
||||
print("默认用户已存在")
|
||||
return
|
||||
|
||||
# 生成随机盐值
|
||||
salt = ''.join(random.choices(string.ascii_letters + string.digits, k=32))
|
||||
|
||||
# 默认密码 123456
|
||||
password = '123456'
|
||||
# 加盐并MD5加密
|
||||
salted_password = hashlib.md5((password + salt).encode('utf-8')).hexdigest()
|
||||
|
||||
# SQL 插入语句
|
||||
sql = """
|
||||
INSERT INTO user
|
||||
(nickname, mobile, email, sex, avatar, login_name, login_pwd, login_salt, status)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||
"""
|
||||
|
||||
# 执行插入
|
||||
cursor.execute(sql, (
|
||||
'Admin', # nickname
|
||||
'13800138000', # mobile
|
||||
'admin@example.com', # email
|
||||
1, # sex
|
||||
'', # avatar
|
||||
'admin', # login_name
|
||||
salted_password, # login_pwd
|
||||
salt, # login_salt
|
||||
1 # status
|
||||
))
|
||||
|
||||
# 提交事务
|
||||
conn.commit()
|
||||
|
||||
print("\n=== 默认用户创建成功 ===")
|
||||
print(f"用户名: admin")
|
||||
print(f"密码: {password}")
|
||||
print("===================")
|
||||
|
||||
except Exception as e:
|
||||
print(f"创建用户失败: {str(e)}")
|
||||
if 'conn' in locals():
|
||||
conn.rollback()
|
||||
finally:
|
||||
if 'cursor' in locals():
|
||||
cursor.close()
|
||||
if 'conn' in locals():
|
||||
conn.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
create_default_user()
|
||||
Reference in New Issue
Block a user