模拟向mysql数据库插入10万条数据

import time
import pymysql
from datetime import datetime, timedelta
class   Insertdata:
	#MySQL数据库连接
    connection = pymysql.connect(host='localhost', user='root', password='kakarott', db='severe_system_8',
                                 port=1900)
    now = datetime.now()
    def sql_execution(self,sql,*args):
        '''

        :param sql:SQL语句
        :param args: 语句中变量(按顺序录入即可)
        :return:
        '''
        try:
            with Insertdata.connection.cursor() as cursor:
                cursor.execute(sql, (args))
                Insertdata.connection.commit()
                # print('数据插入成功!!!')
        except Exception as ex:
            print(ex)
    def clos_data(self):
        try:
            Insertdata.connection.close()
        except Exception as ex:
            print(ex)

inda=Insertdata()
if __name__ == "__main__":
	#sql语句中变量用“%s”标识
    sql = "INSERT  INTO `test_two`(`count`, `sample_id`, `project_id`, `project_name`, `result_id`, `date_time`, `result1`, `result2`, `result3`, `result4`, `flag`, `recalculatedate`, `visable`, `mda_id`, `set_id`, `mda_rate`, `mda_name`, `rate`, `mda_finish`, `calibrationid`, `test_type`, `resultrate`) VALUES (%s, %s, 3, 'TT', '1', %s, '1', '0.0', '****', '', NULL, NULL, '1', -1, NULL, NULL, NULL, NULL, NULL, 0, '0', '1/1|1.0')"
    sql1 = "INSERT INTO `test_one`(`count`, `sample_id`, `number`, `date_time`, `Sample_time`, `name`, `flag1`, `flag2`, `flag3`, `flag4`, `type`, `doctor`, `rack`, `hole`, `provity`, `level`, `time`) VALUES (%s, %s, '2', %s, %s, NULL, '1', NULL, '1', NULL, 'S', NULL, '30', '3', '0', '0|0|0', '')"
    for i in range(100000):
        count = i+2000
        sample_id = 'SQL' + str(20240816999999 -i)
        S = i*284
        S1 = S-428
        date_time = (Insertdata.now -  timedelta(seconds=S)).strftime("%Y-%m-%d %H:%M:%S")
        date_time1 =(Insertdata.now - timedelta(seconds=S1)).strftime("%Y-%m-%d %H:%M:%S")
        inda.sql_execution(sql,count,sample_id,date_time)
        inda.sql_execution(sql1, count, sample_id, date_time1,date_time)
        time.sleep(0.0066)
        print('第',i+1,'条数据插入成功!!!','        ',date_time,'       ',date_time1)
    print('--- 数据插入完成 ---')
    inda.clos_data()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值