本篇将采用 Python 脚本的方式进行批量给mysql造数据。
为了使 Python 可以连上数据库(MySQL),并且可以与数据库交互(增删改查等操作),则需要安装 MySQL 客户端操作库。 命令行安装命令:pip install pymysql
脚本模板:
import pymysql
import time
# 批量新增数据
def insert_data():
# 数据库连接
db_connect = pymysql.connect(
user="xxxxxx",
passwd="xxxxxx",
host="xxxxxx",
db="xxxxxx",
cursorclass=pymysql.cursors.DictCursor)
# 打开数据库
cursor = db_connect.cursor()
my_id_total = ""
for i in range(10):
print("执行次数:" + str(i + 1))
my_int = 4211820 + i + 1
# print("累加数字:" + str(my_int))
# 当前id
my_id = "xxxxxx20220" + str(my_int)
print("当前id: " + my_id)
# id集合
my_id_total = my_id_total + my_id + ","
# 执行sql操作
try:
sql2 = "INSERT INTO `xxxxxx` VALUES (NULL, '{}', 14, 0, NULL, '北京市', '北京市', 2, NULL, NULL, '2022-04-19 17:35:59', '2022-04-19 17:35:59');"
sql = sql2.format(my_id)
# print("新增SQL:" + sql)
cursor.execute(sql)
db_connect.commit()
except Exception as data:
print('Error: 执行查询失败,%s' % data)
time.sleep(0.1)
print("所有id: " + my_id_total)
# 关闭数据库
db_connect.close()
if __name__ == '__main__':
insert_data()