| 22301110 | e361be5 | 2025-06-08 15:24:14 +0800 | [diff] [blame] | 1 | import pymysql |
| 2 | import bcrypt |
| 3 | import config # 使用你已有的 config.get_db_connection() |
| 4 | |
| 5 | def insert_test_user(): |
| 6 | email = '22301110@bjtu.edu.cn' |
| 7 | password = '123456' |
| 8 | hashed_pwd = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode() |
| 9 | |
| 10 | try: |
| 11 | conn = config.get_db_connection() |
| 12 | cursor = conn.cursor() |
| 13 | |
| 14 | cursor.execute(""" |
| 15 | INSERT INTO sys_user ( |
| 16 | user_name, nick_name, password, email, create_time, del_flag, status, user_type |
| 17 | ) VALUES (%s, %s, %s, %s, NOW(), '0', '0', '00') |
| 18 | """, (email, email, hashed_pwd, email)) |
| 19 | |
| 20 | conn.commit() |
| 21 | print("✅ 用户插入成功") |
| 22 | except Exception as e: |
| 23 | print("❌ 插入失败:", e) |
| 24 | finally: |
| 25 | cursor.close() |
| 26 | conn.close() |
| 27 | |
| 28 | |
| 29 | def delete_test_user(): |
| 30 | email = '22301110@bjtu.edu.cn' |
| 31 | |
| 32 | try: |
| 33 | conn = config.get_db_connection() |
| 34 | cursor = conn.cursor() |
| 35 | |
| 36 | cursor.execute("DELETE FROM sys_user WHERE email = %s", (email,)) |
| 37 | conn.commit() |
| 38 | print("🗑️ 用户删除成功") |
| 39 | except Exception as e: |
| 40 | print("❌ 删除失败:", e) |
| 41 | finally: |
| 42 | cursor.close() |
| 43 | conn.close() |
| 44 | |
| 45 | |
| 46 | if __name__ == '__main__': |
| 47 | # 测试:插入 |
| 48 | #insert_test_user() |
| 49 | |
| 50 | # 测试:删除 |
| 51 | delete_test_user() |