如何使用psycopg2.extras.execute_values上传ON CONFLICT

时间:2018-05-12 22:29:25

标签: python-3.x postgresql psycopg2 postgresql-9.6

Python 3.6
psycopg2 2.7.3.1
PostgreSQL 9.6

您好

我正在尝试改进我将数据插入数据库的方式,并尝试在UPDATE上使用psycopg2.extras.execute_values发现here和INSERT ... ON CONFLICT ... UPDATE结合示例示例找到here。我对此很新,所以不排除我在这里遗漏的明显事物。

代码如下:

for zip_file in os.listdir():
    if zipfile.is_zipfile(zip_file):
        logger.info("Processing %s", zip_file)
        for member in zipfile.ZipFile(zip_file).namelist():
            now = dt.datetime.now()
            prices_list = []
            zipfile.ZipFile(zip_file).extract(member, path=temp)
            local_xml = os.path.join(temp, member)
            tree = ET.parse(local_xml)
            root = tree.getroot()
            ns = root.tag[:-4]
            for finhist in root.findall("./%sFinancialHistory" % ns):
                asset_id = int(finhist.get("Id"))
                logger.debug("Processing %s", asset_id)
                for prices in finhist.findall("./%sPrices" % ns):
                    price_currency = prices.get("Currency")
                    for priceset in prices.findall("./%sPriceSet" % ns):
                        price_date = priceset.get("Date")
                        for price in priceset.findall("./%sPrice" % ns):
                            price_value = float(price.text)
                            price_type = price.get("Type")
                            prices_list.append((asset_id, price_date, price_type, price_value, price_currency, now, zip_file))
            try:
                os.remove(local_xml)
            except Exception:
                logger.error("File cannot be deleted", exc_info=True)
            cur = conn.cursor()
            try:
                execute_values(cur, 'WITH VALUES %s AS data (asset_id, price_date, price_type, price_value, price_currency, now, zip_file) \
                           INSERT INTO "LTSF"."Prices"("Asset_ID", "Price_Date", "Price_Type", "Price_Value", "Price_Currency", "Mod_Date", "Zip_File") \
                           data.asset_id, data.price_date, data.price_type, data.price_value, data.price_currency, data.now) \
                           ON CONFLICT \
                           UPDATE "LTSF"."Prices" \
                           "Price_Value"=data.price_value, "Price_Currency"=data.price_currency, "Mod_Date"=data.now, "Zip_File"=data.zip_file \
                           WHERE "Asset_ID"=data.asset_id, "Price_Date"=data.price_date, "Price_Type"=data.price_type', prices_list)
            except Exception:
                logger.error("Problem upserting Prices", exc_info=True)
            conn.commit()
            cur.close()

我回来的错误,具体来说,如下所示,让我觉得它不喜欢我的WITH ... AS部分,但这是基于PostgreSQL维基部分:

Traceback (most recent call last):
  File "C:/Users/u0136211/Documents/LTSF/LTSF Model Import.py", line 76, in <module>
    WHERE "Asset_ID"=data.asset_id, "Price_Date"=data.price_date, "Price_Type"=data.price_type', prices_list)
  File "C:\Users\u0136211\AppData\Local\Continuum\Anaconda3\lib\site-packages\psycopg2\extras.py", line 1256, in execute_values
    cur.execute(b''.join(parts))
psycopg2.ProgrammingError: syntax error at or near "68082102"
LINE 1: WITH VALUES (68082102,'2018-04-30','Nav',2.01275,'BRL','2018...

非常感谢这里的任何指针,因为我需要一种比我迄今为止更有效的upserting方式,并且非常高兴看到execute_values在执行简单插入时所做的差异。

谢谢!

0 个答案:

没有答案