Python3 & 实现向Mysql表中插入10w条不同测试数据

场景:做性能测试,向Mysql数据库,student表中插入10w条测试数据,每条数据不重复。

解决方案:
这里采用Python自动生成SQL,再执行已生成好的SQL往Mysql数据库插入数据。
使用语言:python 3.8
准备SQL 语句,需每条 id 不重复 ,创建student表

create table student(
s_no int primary key auto_increment,
s_name varchar(20) not null,
s_sex varchar(10) not null,
s_birthday datetime,
s_class VARCHAR(20)
);

插入单条数据SQL:

INSERT into student(s_name,s_sex,s_birthday,s_class) VALUES('利玉2','男','1994-08-02','95033');

插入多条数据SQL:

insert into student (s_name,s_sex,s_birthday,s_class) values ('褚饯新', '女', '1978-11-27 00:00:00','95032'), ('蒋斗越', '女', '1971-10-02 00:00:00','95034');

插入多条需要生成多个insert 语句,这里用 Python 语言快速生成插入的SQL脚本,每条SQL后面分号隔开,每次写入数据,最后面加\n 换行,然后拼接执行的sql语句,拼接的时候需注意,最后的字符 ,需改成 ;,用 %s 替换需要变的字段值,如果有多个值都需要变,可以用多个%s替换对应值。用for 循环控制插入的数据数目,因为ID是自动生成,所以不会重复。

学生的姓名、性别、出生日期、班级编号全部自动生成。这里使用Python封装成函数,在拼接SQL时直接引用即可。

在执行代码前先获取当前的时间戳,代码执行完成后再次获取一次时间戳。两次的时间间隔,就是执行的时间了,时间单位是s.

在cmd命令行执行命令安装pymysql:

pip3 install pymysql -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

具体代码如下:

from  pymysql import *
import time
import random

dbinfo = {
    "host": "localhost",
    "user": "root",
    "password": "123123",
    "port": 3306}
class DbConnect():

    def  __init__(self,db_cof,database=""):
        self.db_cof = db_cof
        self.db = connect(database=database,cursorclass=cursors.DictCursor,**db_cof,charset='utf8')
        self.cursor = self.db.cursor()

    def select(self,sql):
        self.cursor.execute(sql)
        results = self.cursor.fetchall()
        return results

    def execute(self,sql):
        try:
            self.cursor.execute(sql)
            self.db.commit()
        except:
            self.db.rollback()
            print("发生回滚")

    def close(self):
        self.db.close()

    # 随机生成出生日期
    def get_birthday(self):
        # 随机生成年月日
        year = random.randint(1960, 2000)
        month = random.randint(1, 12)
        # 判断每个月有多少天随机生成日
        if year % 4 == 0:
            if month in (1, 3, 5, 7, 8, 10, 12):
                day = random.randint(1, 31)
            elif month in (4, 6, 9, 11):
                day = random.randint(1, 30)
            else:
                day = random.randint(1, 29)
        else:
            if month in (1, 3, 5, 7, 8, 10, 12):
                day = random.randint(1, 31)
            elif month in (4, 6, 9, 11):
                day = random.randint(1, 30)
            else:
                day = random.randint(1, 28)
        # 小于10的月份前面加0
        if month < 10:
            month = '0' + str(month)
        if day < 10:
            day = '0' + str(day)
        birthday = str(year) +'-'+ str(month) +'-'+ str(day)+' '+'00:00:00'
        return birthday

    def get_name(self):
        list_Xing = ['赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈', '褚', '卫', '蒋', '沈', '韩', '杨', '张', '李']
        list_Ming = ['豫', '章', '故', '郡', '洪', '都', '新', '府', '星', '分', '翼', '轸', '地', '接', '衡', '庐', '襟', '三', '江', '',
                     '而', '带', '五', '湖', '控', '蛮', '荆', '而', '引', '瓯', '越', '物', '华', '天', '宝', '龙', '光', '射', '牛',
                     '斗', '之', '墟', '人', '杰', '地', '灵', '徐', '孺', '饯', '子']
        name = random.choice(list_Xing)+random.choice(list_Ming)+random.choice(list_Ming)
        return name

    def get_sex(self):
        list_Sex = ['男', '女']
        sex = random.choice(list_Sex)
        return sex

    def get_class(self):
        list_Class = ['95033', '95032', '95034', '95038']
        classs = random.choice(list_Class)
        return classs

    def test(self):
        db = DbConnect(dbinfo,database='selecttest')
        insert_sql = "insert into student (s_name,s_sex,s_birthday,s_class) values "
        insert_values ="".join(["('"+db.get_name()+ "', "
                         "'"+db.get_sex()+"', "
                         "'"+db.get_birthday()+"',"
                         "'"+db.get_class()+"'),\n"
                                for i in range(100000)])
        print(insert_values[:-2])
        sql = insert_sql+insert_values[:-2]+";"
        print(sql)

        time1 = time.time()

        db.execute(sql)
        db.close()

        time2 = time.time()
        print("总过耗时:%s" % (time2-time1))

if __name__ == '__main__':
    DbConnect.test(DbConnect)

以下是插入10条的执行结果:

insert into student (s_name,s_sex,s_birthday,s_class) values ('蒋星', '男', '1971-09-21 00:00:00','95038'),
('韩接斗', '男', '1962-02-08 00:00:00','95038'),
('卫瓯接', '男', '1994-08-03 00:00:00','95034'),
('卫子孺', '女', '1983-12-16 00:00:00','95032'),
('李府饯', '女', '1990-06-29 00:00:00','95032'),
('沈华徐', '女', '1998-09-15 00:00:00','95033'),
('赵孺郡', '女', '1990-07-12 00:00:00','95034'),
('韩牛分', '女', '1982-09-27 00:00:00','95038'),
('钱之湖', '女', '1981-02-17 00:00:00','95038'),
('李分天', '男', '1969-08-15 00:00:00','95034');
总过耗时:0.18653035163879395

进入Navicat,查询student表,已正确插入


image.png

你可能感兴趣的:(Python3 & 实现向Mysql表中插入10w条不同测试数据)