# -*- coding: utf-8 -*-
# date=2020/1/21
import time
import pymysql
import pymysql.cursors
# 获取一个数据库连接,注意如果是UTF-8类型的,需要制定数据库
# port 必须是数字不能为字符串
connection = pymysql.connect(host=‘127.0.0.01‘, user=‘root‘, password=‘root‘, db=‘test‘, port=3306, charset=‘utf8‘)
count = 3
num = 1000000
try:
# 获取一个游标
with connection.cursor() as cursor:
start = time.time()
for i in range(count):
sql_1 = "INSERT INTO t_teacher (name, age) VALUES "
sql_2 = ""
for i in range(num):
sql_2 += "( ‘zhang‘, 2 ),"
sql = sql_1 + sql_2
cursor.execute(sql[0:len(sql) - 1])
connection.commit()
end = time.time()
print("已插入数据%sw" % (count * num / 10000), "用时%ss" % (end - start))
finally:
connection.close()
原文:https://www.cnblogs.com/zhangzhaolin/p/12334815.html