Home

Python使用MySQL数据库实例-插入大量数据

2020/01/30

1、目的:

向数据库插入大量的数据(对数据库性能测试,测试数据库压力)

2、代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import MySQLdb
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='root',db='student',charset='utf8')
#创建一个游标对象
c = conn.cursor()
for i in range(100):
command = "INSERT INTO classes (class_id,class_name) VALUES ("+str(i+1)+",'班级"+str(i+1)+"');"
c.execute(command)
#修改数据库后需要一个commit()操作
conn.commit()
#关闭连接
conn.close()

结果:
image