Table of Contents
1. Batch data insertion scenario
2. Selection of tools for inserting data
3. Select Python for batch insertion
Home Database Mysql Tutorial How to implement batch new data in Mysql database in python

How to implement batch new data in Mysql database in python

May 29, 2023 pm 10:34 PM
mysql 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()
Copy after login

The data is displayed as follows:

How to implement batch new data in Mysql database in python

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? Apr 01, 2025 pm 03:03 PM

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...

How to efficiently integrate Node.js or Python services under LAMP architecture? How to efficiently integrate Node.js or Python services under LAMP architecture? Apr 01, 2025 pm 02:48 PM

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...

What is the reason why pipeline persistent storage files cannot be written when using Scapy crawler? What is the reason why pipeline persistent storage files cannot be written when using Scapy crawler? Apr 01, 2025 pm 04:03 PM

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...

What is the reason why the Python process pool handles concurrent TCP requests and causes the client to get stuck? What is the reason why the Python process pool handles concurrent TCP requests and causes the client to get stuck? Apr 01, 2025 pm 04:09 PM

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. ...

Python Cross-platform Desktop Application Development: Which GUI Library is the best for you? Python Cross-platform Desktop Application Development: Which GUI Library is the best for you? Apr 01, 2025 pm 05:24 PM

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...

Python hourglass graph drawing: How to avoid variable undefined errors? Python hourglass graph drawing: How to avoid variable undefined errors? Apr 01, 2025 pm 06:27 PM

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...

How to view the original functions encapsulated internally by Python functools.partial object? How to view the original functions encapsulated internally by Python functools.partial object? Apr 01, 2025 pm 04:15 PM

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

How to optimize processing of high-resolution images in Python to find precise white circular areas? How to optimize processing of high-resolution images in Python to find precise white circular areas? Apr 01, 2025 pm 06:12 PM

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...

See all articles