How to implement batch new data in Mysql database in python
1. Batch data insertion scenario
When performing data pressure, a large amount of data needs to be tested
For example, logging in Thousands of users log in at the same time
For example, data processing requires us to insert database data because the source data is not available
Selection method
Use Jmeter to add interface data in batches
Use stored procedures for direct database operations
Use Python performs database operations
2. Selection of tools for inserting data
The selection method should be selected according to the actual situation. It is not which one is better, but which one A more efficient way to solve our problems, for example;
When we need Jmeter operations to actually add users in batches, and users need to upload images, what should we do at this time? The choice?
If we choose to use python, we may need to read file operations, obtain names, fill in various fields, etc., which will be more troublesome
If you choose a stored procedure, it is a bit unrealistic. How to use a stored procedure to upload avatars?
So we prefer to choose a simple and convenient one, which is our jmeter
Back to our topic, if we want to batch insert data, what should we choose? Well, just for database operations, we can actually choose either stored procedures or python. I personally prefer python because it is commonly used for automation and is more convenient, so the follow-up will use python as an example to explain the database.
3. Select Python for batch insertion
Take the local Mysql database as an example
#安装操作数据库的第三方包 C:\Users\Lenovo> pip install pymsql #全文使用Pycharm进行操作 ------------------------------------------------ #导入数据库操作包 import pymysql #数据库的基本信息[主机、用户名、密码、端口号、连接的数据库] Host= '127.0.0.1' user = 'root' pwd='123456' port = 3306 database ='sq' #进行创建数据库的连接 conn = pymysql.connect(host=Host,user=user,password=pwd,port=port,db=database) #获取游标 cursor = conn.cursor() #使用format对sql语句进行参数化 sql = "insert into takeout_food values('0{j}','testautoinsert{i}','10','this is auto test','17.jpg');" #执行数据库的插入语句 j=17 for i in range(1,10): j+=1 data = cursor.execute(sql.format(i=i,j=j)) #连接实例进行数据的提交 conn.commit() #关闭游标 cursor.close()
The data is displayed as follows:
The above is the detailed content of How to implement batch new data in Mysql database in python. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The page is blank after PHP connects to MySQL, and the reason why die() function fails. When learning the connection between PHP and MySQL database, you often encounter some confusing things...

Many website developers face the problem of integrating Node.js or Python services under the LAMP architecture: the existing LAMP (Linux Apache MySQL PHP) architecture website needs...

When using Scapy crawler, the reason why pipeline persistent storage files cannot be written? Discussion When learning to use Scapy crawler for data crawler, you often encounter a...

Python process pool handles concurrent TCP requests that cause client to get stuck. When using Python for network programming, it is crucial to efficiently handle concurrent TCP requests. ...

Choice of Python Cross-platform desktop application development library Many Python developers want to develop desktop applications that can run on both Windows and Linux systems...

Getting started with Python: Hourglass Graphic Drawing and Input Verification This article will solve the variable definition problem encountered by a Python novice in the hourglass Graphic Drawing Program. Code...

Deeply explore the viewing method of Python functools.partial object in functools.partial using Python...

How to handle high resolution images in Python to find white areas? Processing a high-resolution picture of 9000x7000 pixels, how to accurately find two of the picture...
