# -*- coding: UTF-8 -*- # Author : LinYaoHong # Date : 2019/8/15 14:43 # Tools : PyCharm # pip install pymysql -i https://pypi/douban.com/simple import pymysql conn = pymysql.connect(host='localhost', port=3306, user="root", password='111111', db="test") cursor = conn.cursor() # 批量插入数据 li = [ ('lin', 'man', 19, '151512152'), ('yao', 'man', 19, '151515153'), ('I', 'man', 19, '151512152'), ('LOVE', 'man', 19, '151515153'), ('You', 'man', 19, '151512152'), ('L', 'man', 19, '151515153') ] # 批量插入 会开启事务,只有提交才会写入数据库 n = 1 while n < 2: sql = "insert into userinfo1(name,sex,age,tel) values(%s,%s,%s,%s)" # cursor.executemany('insert into userinfo1(name,sex,age,tel) values(%s,%s,%s,%s)', li) cursor.executemany(sql, li) # executemany 批量插入的时候使用 n += 1 conn.commit() cursor.close() conn.close()