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()
模拟向mysql数据库插入10万条数据
于 2024-08-21 18:15:09 首次发布