import sqlite3
from faker import Faker
import random
# 初始化 Faker 库
fake = Faker()
# 创建 SQLite 数据库连接
conn = sqlite3.connect('user_data.db')
cursor = conn.cursor()
# 创建表格(如果不存在)
cursor.execute('''
CREATE TABLE IF NOT EXISTS UserData (
id INTEGER PRIMARY KEY AUTOINCREMENT,
type INTEGER,
username TEXT,
email TEXT,
password TEXT,
gender INTEGER,
age INTEGER,
birthDate TEXT,
department TEXT,
position TEXT,
contactNumber TEXT,
entry TEXT,
baseSalary INTEGER,
attendanceDeduction INTEGER,
performanceSalary INTEGER,
actualSalary INTEGER,
lateCount INTEGER,
leaveCount INTEGER,
absenteeismCount INTEGER,
overtimeHours INTEGER,
attendanceScore INTEGER,
workFamiliarityScore INTEGER,
organizationalSkillScore INTEGER,
executionSkillScore INTEGER,
satisfactionScore INTEGER,
complaintRate INTEGER,
problemFindingScore INTEGER,
problemSolvingScore INTEGER
)
''')
# 随机数据生成
def generate_random_data(type_):
return {
"type": type_, # type 根据传入的参数设置
"username": "admin" if type_ == 0 else fake.user_name(),
"email": fake.email(),
"password": "admin" if type_ == 0 else fake.password(),
"gender": random.choice([0, 1]), # 0: 女, 1: 男
"age": random.randint(18, 60),
"birthDate": fake.date_of_birth(minimum_age=18, maximum_age=60).strftime('%Y-%m-%d'),
"department": fake.word(),
"position": fake.job(),
"contactNumber": fake.phone_number(),
"entry": fake.date_this_decade().strftime('%Y-%m-%d'),
"baseSalary": random.randint(3000, 15000),
"attendanceDeduction": random.randint(0, 500),
"performanceSalary": random.randint(0, 5000),
"actualSalary": random.randint(3000, 15000),
"lateCount": random.randint(0, 10),
"leaveCount": random.randint(0, 10),
"absenteeismCount": random.randint(0, 5),
"overtimeHours": random.randint(0, 50),
"attendanceScore": random.randint(1, 100),
"workFamiliarityScore": random.randint(1, 100),
"organizationalSkillScore": random.randint(1, 100),
"executionSkillScore": random.randint(1, 100),
"satisfactionScore": random.randint(1, 100),
"complaintRate": random.randint(1, 100),
"problemFindingScore": random.randint(1, 100),
"problemSolvingScore": random.randint(1, 100)
}
# 插入1000条随机数据
for _ in range(1000):
data = generate_random_data(1)
cursor.execute('''
INSERT INTO UserData (
type, username, email, password, gender, age, birthDate, department, position,
contactNumber, entry, baseSalary, attendanceDeduction, performanceSalary,
actualSalary, lateCount, leaveCount, absenteeismCount, overtimeHours,
attendanceScore, workFamiliarityScore, organizationalSkillScore,
executionSkillScore, satisfactionScore, complaintRate, problemFindingScore,
problemSolvingScore
) VALUES (
:type, :username, :email, :password, :gender, :age, :birthDate, :department, :position,
:contactNumber, :entry, :baseSalary, :attendanceDeduction, :performanceSalary,
:actualSalary, :lateCount, :leaveCount, :absenteeismCount, :overtimeHours,
:attendanceScore, :workFamiliarityScore, :organizationalSkillScore,
:executionSkillScore, :satisfactionScore, :complaintRate, :problemFindingScore,
:problemSolvingScore
)''', data)
# 添加 type = 0 的记录(用户名和密码都是 'admin')
admin_data = generate_random_data(0)
cursor.execute('''
INSERT INTO UserData (
type, username, email, password, gender, age, birthDate, department, position,
contactNumber, entry, baseSalary, attendanceDeduction, performanceSalary,
actualSalary, lateCount, leaveCount, absenteeismCount, overtimeHours,
attendanceScore, workFamiliarityScore, organizationalSkillScore,
executionSkillScore, satisfactionScore, complaintRate, problemFindingScore,
problemSolvingScore
) VALUES (
:type, :username, :email, :password, :gender, :age, :birthDate, :department, :position,
:contactNumber, :entry, :baseSalary, :attendanceDeduction, :performanceSalary,
:actualSalary, :lateCount, :leaveCount, :absenteeismCount, :overtimeHours,
:attendanceScore, :workFamiliarityScore, :organizationalSkillScore,
:executionSkillScore, :satisfactionScore, :complaintRate, :problemFindingScore,
:problemSolvingScore
)
''', admin_data)
# 提交事务并关闭连接
conn.commit()
conn.close()
print("数据生成完毕,已保存至 user_data.db 文件。")
04-14
03-14
7044
