Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add data to Mysql Database in batch by python

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

The main content of this article is to explain "python how to achieve Mysql database batch new data", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "python how to achieve Mysql database batch add data" it!

I. the scenario of inserting data in batches

A large amount of data testing is needed when data pressure is carried out.

For example, login requires thousands of users to log in at the same time.

For example, in data processing, because there is no source data, we need to insert database data.

Selection method

Use Jmeter to add interface data in batch

Use stored procedures for direct operation of the database

Use Python to operate the database

2. Selection of tools for inserting data

The choice method should be made according to the actual situation, not which is better, but which can solve our problems more quickly, for example, chestnut.

When we need Jmeter operation, we need to add a large number of users, and users need to upload images, how should we choose?

If we choose to use python, we may need to read the file operation, get the name, fill in each field and so on, which will be troublesome.

If you choose a stored procedure, it's a bit unrealistic. How can you use a stored procedure to upload an avatar?

So we prefer to choose a simple and convenient one, that is, our jmeter.

Back to our topic, if the bulk insertion of data, how should we choose, only for database operations, in fact, we can choose stored procedures or python, I personally prefer python because it usually uses more automation, more convenient, so the follow-up is to take python as an example to explain the database.

3. Select Python for batch insertion

Take the local Mysql database as an example

# install the third-party package C:\ Users\ Lenovo > pip install pymsql# to operate the database using Pycharm-# Import the basic information of the database operation package import pymysql# database [host, user name, password, port number, Connected database] Host= '127.0.0.1'user =' root'pwd='123456'port = 3306database = 'sq'# to create a database connection conn = pymysql.connect (host=Host User=user,password=pwd,port=port,db=database) # get the cursor cursor = conn.cursor () # parameterize the sql statement using format sql = "insert into takeout_food values ('0 {j}', 'testautoinsert {I}','10 percent charge this is auto test','17.jpg') "# execute the insert statement j=17for i in range of the database (1Power10): jackers data 1 data = cursor.execute (sql.format (isancidjobj)) # connect the instance to submit the data () # close the cursor cursor.close ()

The data are shown as follows:

At this point, I believe you have a deeper understanding of "python how to achieve Mysql database batch new data", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report