本篇将采用 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()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~