本文介绍了我如何使用executemany在MySQL中插入Python字典列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我目前正在使用MySQL和Python从网络上抓取数据.具体来说,我是在抓取表数据并将其插入数据库中.我当前的解决方案有效,但是我觉得它效率极低,如果不重写代码,很可能会锁定数据库.这是我目前使用的(部分代码):

I'm currently using MySQL and Python to scrape data from the web. Specifically, I am scraping table data and inserting it into my database. My current solution works, but I feel it is extremely inefficient and will most likely lock up my database if I don't rewrite the code. Here is what I currently use (partial code):

itemBank = []
for row in rows:
    itemBank.append((tempRow2,tempRow1,tempRow3,tempRow4)) #append data

#itemBank List of dictionaries representing data from each
row of the table. i.e.
('Item_Name':"Tomatoes",'Item_Price':"10",'Item_In_Stock':"10",'Item_Max':"30")

for item in itemBank:
    tempDict1 = item[0]
    tempDict2 = item[1]
    tempDict3 = item[2]
    tempDict4 = item[3]

    q = """ INSERT IGNORE INTO
         TABLE1
        (
           Item_Name,
           Item_Price,
           Item_In_Stock,
           Item_Max,
           Observation_Date
         ) VALUES (
           "{0}",
           "{1}",
           "{2}",
           "{3}",
           "{4}"
           )
        """.format(tempDict1['Item_Name'],tempDict2['Item_Price'],tempDict3['Item_In_Stock'],
                   tempDict4['Item_Max'],getTimeExtra)

    try:
        x.execute(q)
        conn.commit()
    except:
        conn.rollback()

执行表的每一行都很麻烦.我尝试使用executemany,但似乎无法弄清楚如何正确访问字典的值.因此,在给定数据结构的情况下,如何在此处使用executemany插入数据库?

Executing each row of the table is cumbersome. I've tried using executemany, but I can't seem to figure out how to access the values of the dictionaries correctly.So, how can I use executemany here to insert into the database given the structure of my data?

推荐答案

itemBank = []
for row in rows:
    itemBank.append((
        tempRow2['Item_Name'],
        tempRow1['Item_Price'],
        tempRow3['Item_In_Stock'],
        tempRow4['Item_Max'],
        getTimeExtra
        )) #append data


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

try:
    x.executemany(q, itemBank)
    conn.commit()
except:
    conn.rollback()

希望它将对您有帮助

这篇关于我如何使用executemany在MySQL中插入Python字典列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 20:15