File_upload_system_Guangdon.../insert_data_to_database.py
superlishunqin 68b99755ec ALL
2024-11-14 15:46:37 +08:00

40 lines
1013 B
Python

import json
import mysql.connector
from flask_bcrypt import Bcrypt
bcrypt = Bcrypt()
# 读取 JSON 数据
with open('students_config.json', 'r', encoding='utf-8') as f:
students_config = json.load(f)
# 连接到 MySQL 数据库
conn = mysql.connector.connect(
host='8.218.165.242',
user='sure_001',
password='EKKWLMmrGmG7sdPf',
database='sure_001'
)
cursor = conn.cursor()
# 创建 students 表
cursor.execute('''
CREATE TABLE IF NOT EXISTS students (
id VARCHAR(255) PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL
)
''')
# 插入数据并哈希默认密码
default_password_hash = bcrypt.generate_password_hash('skd123456').decode('utf-8')
for student in students_config['students']:
cursor.execute('INSERT INTO students (id, name, email, password) VALUES (%s, %s, %s, %s)',
(student['id'], student['name'], student['email'], default_password_hash))
conn.commit()
cursor.close()
conn.close()