Home >Database >Mysql Tutorial >How Can I Optimize MySQL Insertion of Dictionary Lists Using `executemany` in Python?

How Can I Optimize MySQL Insertion of Dictionary Lists Using `executemany` in Python?

Barbara Streisand
Barbara StreisandOriginal
2024-11-10 15:10:03943browse

How Can I Optimize MySQL Insertion of Dictionary Lists Using `executemany` in Python?

Efficient MySQL Insertion of Dictionary Lists Using executemany in Python

Problem:

When scraping data from web tables into a MySQL database, an inefficient approach is employed, involving individual row insertions. The goal is to optimize this process using executemany.

Solution:

The executemany method allows for the insertion of multiple rows of data into a database table at once. In this case, the data is structured as a list of dictionaries, each representing a row of the table.

Instead of accessing the values of the dictionaries individually, as shown in the original code, the data should be appended to a list in the following format:

itemBank.append((
    tempRow2['Item_Name'],
    tempRow1['Item_Price'],
    tempRow3['Item_In_Stock'],
    tempRow4['Item_Max'], 
    getTimeExtra
)) #append data

The executemany method can then be used with the following query:

q = """ insert ignore into TABLE1 (
        Item_Name, Item_Price, Item_In_Stock, Item_Max, Observation_Date ) 
        values (%s,%s,%s,%s,%s)           
    """

Replacing the individual row insertions with the executemany method will significantly improve the efficiency of the database insertion process.

The above is the detailed content of How Can I Optimize MySQL Insertion of Dictionary Lists Using `executemany` in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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