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 insert data in batches in the database

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article, the editor introduces in detail "how to insert data in batches in the database". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to insert data in batches in the database" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.

Batch insert

Bulk insert uses the executemany () method, whose second parameter is a list of tuples containing the data we want to insert:

Demo_mysql_test.py:

Insert multiple records into the sites table.

Import mysql.connector

Mydb = mysql.connector.connect (

Host= "localhost"

User= "root"

Passwd= "123456"

Database= "runoob_db"

)

Mycursor = mydb.cursor ()

Sql = "INSERT INTO sites (name, url) VALUES (% s,% s)"

Val = [('Google',' https://www.google.com'), ('Github',' https://www.github.com'), ('Taobao',' https://www.taobao.com'), ('stackoverflow',' https://www.stackoverflow.com/')])

Mycursor.executemany (sql, val)

Mydb.commit () # the contents of the data table are updated and this statement must be used

Print (mycursor.rowcount, "record inserted successfully."

)

Execute the code and the output is as follows:

4the record was inserted successfully.

After executing the above code, we can look at the record of the data table:

If we want to get the ID of the data record after it has been inserted, we can use the following code:

Demo_mysql_test.py:

Import mysql.connector

Mydb = mysql.connector.connect (

Host= "localhost"

User= "root"

Passwd= "123456"

Database= "runoob_db"

)

Mycursor = mydb.cursor ()

Sql = "INSERT INTO sites (name, url) VALUES (% s,% s)"

Val = ("Zhihu", "https://www.zhihu.com")"

Mycursor.execute (sql, val)

Mydb.commit ()

Print ("1 record inserted, ID:", mycursor.lastrowid)

Execute the code and the output is as follows:

1 record has been inserted, ID: 6 read here, this article "how to insert data in a batch in the database" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more related articles, welcome to follow the industry information channel.

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