到目前为止,我已经尝试了在这里找到的任何解决方案,但是没有一个解决方案解决了我无法使用 psycopg2 将数据插入 azure Postgres 的问题。
这是我想要插入的示例字典:
response_data = {
"product_id" : "Test0",
"ean": product_data["ean"],
"sku": product_data["sku"],
"product_title": product_data["product_name"],
"category": product_data["category"],
"seller": product_data["seller"],
"current_price": product_data["current_price"],
"uvp": product_data["original_price"],
"source" : "Google",
"availability" : product_data["availability"],
"discount": product_data["discount"],
"product_details": product_data["details"],
"description": product_data["description"],
"avg_stars": float(product_data["avg_stars"]),
"recommendation_rate": int(product_data["recommendation_rate"]),
"image_url": product_data["image_url"],
"url": 'url',
"product_url": "product_url"
}
有些值又是字典。 这就是我插入值的方式:
insert_sql = """
INSERT INTO products.product_data(
product_id, image_url, product_url, url, source,
ean, sku, product_title,
uvp, current_price, discount,
seller, availability, product_details,
description, avg_stars, recommendation_rate, reviews,
crawling_timestamp
)
VALUES (
%(product_id)s, %(image_url)s, %(product_url)s, %(url)s, %(source)s,
%(ean)s, %(sku)s, %(product_title)s,
%(uvp)s, %(current_price)s, %(discount)s,
%(seller)s, %(availability)s, %(product_details)s,
%(description)s, %(avg_stars)s, %(recommendation_rate)s, %(reviews)s,
NOW()
);
"""
cursor.execute(insert_sql, response_data)
使用此代码我收到错误:
psycopg2.ProgrammingError: can't adapt type 'dict'
我有示例响应数据,如下所示:
response_data = {
"product_id": "Test1",
"image_url": {
"url1": "image_url1",
"url2": "image_url2"
},
"product_url": {
"url1": "product_url1",
"url2": "product_url2"
},
"url": {
"url1": "url1",
"url2": "url2"
},
"source": "Google",
"ean": {
"ean1": "ean_value1",
"ean2": "ean_value2"
},
"sku": {
"sku1": "sku_value1",
"sku2": "sku_value2"
},
"product_title": "Product Title",
"uvp": {
"original": 950.00,
"discounted": 800.00
},
"current_price": {
"price1": 900.00,
"price2": 850.00
},
"discount": {
"discount1": "10%",
"discount2": "5%"
},
"seller": {
"seller1": "Seller 1",
"seller2": "Seller 2"
},
"availability": {
"availability1": "In Stock",
"availability2": "Out of Stock"
},
"product_details": {
"detail1": "Product Detail 1",
"detail2": "Product Detail 2"
},
"description": {
"description1": "Product Description 1",
"description2": "Product Description 2"
},
"avg_stars": 4.5, # Numeric value
"recommendation_rate": 90, # Numeric value
"reviews": {
"review1": "Positive",
"review2": "Negative"
}
}
我尝试将数据插入到
product_data
表中,其中包含以下列:
product_id TEXT PRIMARY KEY,
image_url JSONB,
product_url JSONB,
url JSONB,
source TEXT,
ean JSONB,
sku JSONB,
product_title JSONB,
uvp JSONB,
current_price JSONB,
discount JSONB,
seller JSONB,
availability JSONB,
product_details JSONB,
description JSONB,
avg_stars FLOAT,
recommendation_rate INTEGER,
reviews JSONB,
crawling_timestamp TIMESTAMPTZ
使用以下代码:
insert_sql = """
INSERT INTO product_data(
product_id, image_url, product_url, url, source,
ean, sku, product_title,
uvp, current_price, discount,
seller, availability, product_details,
description, avg_stars, recommendation_rate, reviews,
crawling_timestamp
)
VALUES (
%(product_id)s, %(image_url)s, %(product_url)s, %(url)s, %(source)s,
%(ean)s, %(sku)s, %(product_title)s,
%(uvp)s, %(current_price)s, %(discount)s,
%(seller)s, %(availability)s, %(product_details)s,
%(description)s, %(avg_stars)s, %(recommendation_rate)s, %(reviews)s,
NOW()
);
我得到了同样的错误,如下所示:
我添加了以下代码来解决该错误:
image_url = Json(response_data['image_url'])
product_url = Json(response_data['product_url'])
url = Json(response_data['url'])
ean = Json(response_data['ean'])
sku = Json(response_data['sku'])
product_title = Json(response_data['product_title'])
uvp = Json(response_data['uvp'])
current_price = Json(response_data['current_price'])
discount = Json(response_data['discount'])
seller = Json(response_data['seller'])
availability = Json(response_data['availability'])
product_details = Json(response_data['product_details'])
description = Json(response_data['description'])
reviews = Json(response_data['reviews'])
当我运行代码时,它执行成功,没有任何错误,如下所示:
行成功插入到
product_data
表中,如下图:
完整代码:
import psycopg2
import json
from psycopg2.extras import Json
response_data = {
}
connection = psycopg2.connect(
host="<serverName>.postgres.database.azure.com",
database="<databaseNmae>",
user="server",
password="<password>"
)
cursor = connection.cursor()
insert_sql = """
INSERT INTO product_data (
product_id, image_url, product_url, url, source,
ean, sku, product_title,
uvp, current_price, discount,
seller, availability, product_details,
description, avg_stars, recommendation_rate, reviews,
crawling_timestamp
)
VALUES (
%(product_id)s, %(image_url)s, %(product_url)s, %(url)s, %(source)s,
%(ean)s, %(sku)s, %(product_title)s,
%(uvp)s, %(current_price)s, %(discount)s,
%(seller)s, %(availability)s, %(product_details)s,
%(description)s, %(avg_stars)s, %(recommendation_rate)s, %(reviews)s,
NOW()
);
"""
image_url = Json(response_data['image_url'])
product_url = Json(response_data['product_url'])
url = Json(response_data['url'])
ean = Json(response_data['ean'])
sku = Json(response_data['sku'])
product_title = Json(response_data['product_title'])
uvp = Json(response_data['uvp'])
current_price = Json(response_data['current_price'])
discount = Json(response_data['discount'])
seller = Json(response_data['seller'])
availability = Json(response_data['availability'])
product_details = Json(response_data['product_details'])
description = Json(response_data['description'])
reviews = Json(response_data['reviews'])
cursor.execute(insert_sql, {
"product_id": response_data['product_id'],
"image_url": image_url,
"product_url": product_url,
"url": url,
"source": response_data['source'],
"ean": ean,
"sku": sku,
"product_title": product_title,
"uvp": uvp,
"current_price": current_price,
"discount": discount,
"seller": seller,
"availability": availability,
"product_details": product_details,
"description": description,
"avg_stars": response_data['avg_stars'],
"recommendation_rate": response_data['recommendation_rate'],
"reviews": reviews
})
connection.commit()
cursor.close()
connection.close()