python使用faker生成随机测试数据

import csv
from sys import prefix

from faker import Faker
import random
import pymysql.cursors
import datetime
import string

def insert_into_deliverypersonnel(data):
#连接数据库
        connection = pymysql.connect(host='localhost',
                                     user='root',
                                     password='',
                                     database='djangoz04j9t0n',
                                     cursorclass=pymysql.cursors.DictCursor)

        try:
            with connection.cursor() as cursor:
                # 执行SQL查询
                sql = "insert into deliverypersonnel(employeeid,name,gender,age,workage,workarea,email,phone,address,deliveryrecord,workefficiency)values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
                cursor.execute(sql, data)
                connection.commit()
        except:
            print('hello')
        finally:
            # 关闭连接
            connection.close()

# ---------------------------------------------------------------

def insert_into_order_data(workarea):

    # 生成订单名称
    prefix = '订单'
    length = 6
    current_date = datetime.datetime.now().strftime('%Y%m%d%H%M%S')  # 精确到秒
    random_str = ''.join(random.choices(string.ascii_uppercase + string.digits, k=length))
    ordername = f"{prefix}_{current_date}_{random_str}"
    print(ordername)

    # 生成订单类型
    order_type_list = ['生鲜','餐饮美食','甜点饮品','商超日用品','鲜花绿植','医药','跑腿服务','垂直消费品类']
    ordertype=order_type_list[random.randint(0,7)]

    order_quantity = random.randint(5, 100)
    orderdistribution= workarea

    quarter_list = ['第1季度', '第2季度', '第3季度', '第4季度']
    quarter = quarter_list[random.randint(0,3)]

    amountofmoney = random.randint(50, 1000)

    connection = pymysql.connect(host='localhost',
                                 user='root',
                                 password='',
                                 database='djangoz04j9t0n',
                                 cursorclass=pymysql.cursors.DictCursor)


    try:
        with connection.cursor() as cursor:
            # 执行SQL查询
            sql = "insert into orderdata(ordername, ordertype, orderquantity, orderdistribution, quarter, amountofmoney)values(%s,%s,%s,%s,%s,%s)"
            cursor.execute(sql, (
            ordername, ordertype, order_quantity, orderdistribution, quarter, amountofmoney))
            connection.commit()
    except:
        print('hello')
    finally:
        # 关闭连接
        connection.close()

# ----------------------------------------------------------------

def insert_into_schedulingsystem():
    sql = "insert into schedulingsystem(ordername, ordertype, employeeid, name, phone, deliveryrecord) select ordername, ordertype, employeeid, name, phone, deliveryrecord from orderdata,deliverypersonnel where orderdata.id = deliverypersonnel.id "

def insert_into_customersatisfaction():
    sql = 'insert into customersatisfaction(ordername, ordertype)select ordername, ordertype from orderdata'

    for i in range(1, 14007):
        print(i)
        name = fake.name()
        print(name)
        satisfaction_list = ['非常满意', '满意', '不满意', '非常不满意']
        satisfaction = satisfaction_list[random.randint(0, 3)]
        print(satisfaction)

        connection = pymysql.connect(host='localhost',
                                     user='root',
                                     password='',
                                     database='djangoz04j9t0n',
                                     cursorclass=pymysql.cursors.DictCursor)

        try:
            with connection.cursor() as cursor:
                # 执行SQL查询
                sql = "update customersatisfaction set customername = %s, satisfaction=%s, evaluationcontent=%s where id=%s"
                cursor.execute(sql, (name, satisfaction, satisfaction, str(i)))
                connection.commit()
                print('执行完毕')
        except:
            print('hello')
        finally:
            # 关闭连接
            connection.close()


# 生成一个随机的电子邮件地址
fake = Faker(["zh_CN"])
Faker.seed(random.randint(0, 100000))
insert_into_customersatisfaction()

# with open('example.csv', 'r', newline='') as file:
#     reader = csv.reader(file)
#     i = 0
#     for row in reader:
#         i = i + 1
#         if i == 1:
#             continue
#
#         id = row[0]
#
#         employeeid = '工号' + id
#         name = row[1]
#         age = row[2]
#         gender = row[3]
#         workage = row[4]
#
#         deliveryrecord=random.randint(50, 1000)
#         workefficiency=random.randint(5, 10)
#
#
#         email = fake.email()
#         print(email)
#         address = fake.address()
#         print(address)
#         workarea = fake.city()
#         print(workarea)
#
#         phone = fake.phone_number()
#         print(phone)

        # insert_into_deliverypersonnel((employeeid,name,gender,age,workage,workarea,email,phone,address,deliveryrecord,workefficiency))
        # insert_into_order_data(workarea)







posted @ 2025-03-26 13:48  zqxLonely  阅读(22)  评论(0)    收藏  举报