python 对MySQL数据库插入数据,并记录插入数据所需的时间
import timefrom sys import pathpath.append(r‘C:\Users\Administrator\ceshi1111\Lib\site-packages\pymysql‘)import pymysqlfrom pymysql import *def timer(func): #:时间模块 def decor(*args): #:decor传参 start_time = time.time() #:记录起始时间 func(*args) #:调用func函数,但其实就是调用add_test_users end_time = time.time() #:结束时间 d_time = end_time - start_time #: 记录总共需要的时间耗时 print("the running time is : ", d_time) #:打印 return decor #:返回decor 装饰器要用到@timer #:装饰器 调用timer 那么此时add_test_user == funcdef add_test_users(n): #:创建add_test_users连接数据库函数 conn = pymysql.connect(host=‘192.168.137.9‘ ,port=3306 ,user=‘root‘, password=‘123123‘, db=‘mysql‘, charset=‘utf8‘) cursor = conn.cursor() #:获取操作游标 for i in range(0, n): #:执行循环次数 try: sql = "insert into test1 values(%s)" param = ((str(i))) cursor.execute(sql, param) #:执行sql except Exception as e: return conn.commit() cursor.close() conn.close() print(‘OK‘)add_test_users(10) #:循环插入10条数据
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30