import pymysql
import random
import string
# 建立数据库连接
mysql = pymysql.connect(host="127.0.0.1", user="root",port=3306,password="", charset='utf8', autocommit=True)
# 建立游标
cursor = mysql.cursor(pymysql.cursors.DictCursor)
runoob_title ='jjj'
runoob_author ='iii'
# 定义要执行的SQL语句
for i in range(1,100000): # for循环来组合新增语句
jbxx_unid =8183 + i # ID从目前数据库中最大的一个开始
# UUID是32位字母加数字组成,所以先生成随机26位字母加数字随机数,然后加上序号保证UUID的唯一性
ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 26))
ran_str2 = ''.join(random.sample('zyxwvutsrqponmlkjihgfedcba',10))
i_len = len(str(i))
runoob_title = str(ran_str2) + "00000" + str(i)
runoob_author = str(ran_str2) + "0000" + str(i)
# if i_len == 1:
# runoob_title = str(ran_str2) +"00000" + str(i)
# runoob_author = str(ran_str2) + "0000" + str(i)
# elif i_len ==2:
# runoob_title = ran_str2.lower() + "00000" + str(i)
# runoob_author = ran_str2.lower() + "0000" + str(i)
# elif i_len == 3:
# jbxx_uuid = ran_str.lower() + "000" + str(i)
# elif i_len == 4:
# jbxx_uuid = ran_str.lower() + "00" + str(i)
# elif i_len == 5:
# jbxx_uuid = ran_str.lower() + "0" + str(i)
# else:
# jbxx_uuid = ran_str.lower() + str(i)
# # 需要插入数据的数据要唯一性的都加循环数字
# jbxx_psid = 9998942509 + i
# jbxx_name = "test" + str(i)
# 新增的SQL语句,用format来替换我们的参数。
sql = """
INSERT INTO `runoob`.`runoob_tbl` (
`runoob_title`,
`runoob_author`,
`submission_date`
)
VALUES
(
'{0}',
'{1}',
'2018-03-23 16:25:37'
);
""".format(runoob_title,runoob_author)
# 执行SQL语句
print(sql)
cursor.execute(sql)
print("成功插入{}条数据!".format(i))
# 关闭光标对象
cursor.close()
# 关闭数据库连接
print("插入完成所有数据!关闭数据库连接!")
mysql.close()
python批量插入mysql
最新推荐文章于 2024-06-01 23:16:49 发布