愿天下再无BUG 2020-06-25
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条数据