标签:pass for admin app value ret import try packages
import time
from sys import path
path.append(r‘C:\Users\Administrator\ceshi1111\Lib\site-packages\pymysql‘)
import pymysql
from 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 == func
def 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条数据
python 对MySQL数据库插入数据,并记录插入数据所需的时间
标签:pass for admin app value ret import try packages
原文地址:https://www.cnblogs.com/yandh/p/13191809.html