python 随机生成1000条数据库(db)文件数据

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 文件。")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值