I have tried any solution I have found here so far, however not a single one solved my issues of not being able to insert data into azure Postgres using psycopg2.
This is the sample dict I have and want to insert:
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"
}
Some of the values are dicts again. This is how I insert the values:
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)
With this code I am getting the error:
psycopg2.ProgrammingError: can't adapt type 'dict'
I have the sample response data as shown below:
I tried to insert data into the
product_data
table with the columns below:With the code below:
I got the same error as shown below:
I added the code below to resolve the error:
When I ran the code, it executed successfully without any error, as shown below:
The rows were inserted into the
product_data
table successfully, as shown below:Complete code: