insert psycopg2 python dictionary as json
Posted: December 1st, 2022, 8:39 am
I want to insert a python dictionary into my postgresql database as a json file (via python and psycopg2).
I have this:
This gives an error:
I'm not sure how to perform this precise thing, as I'm new to psycopg2.
I have this:
Code: Select all
thedictionary = {'price money': '$1', 'name': 'Google', 'color': '', 'imgurl': 'http://www.google.com/images/nav_logo225.png', 'charateristics': 'No Description', 'store': 'google'}
cur.execute("INSERT INTO product(store_id, url, price, charecteristics, color, dimensions) VALUES (%d, %s, %s, %d, %s, %s)", (1, 'http://www.google.com', '$20', thedictionary, 'red', '8.5x11'))
I'm not sure where to go from here. I couldn't locate anything related on the internet, but I did come across this page, which stated that "you may use register adapter() to adapt any Python dictionary to JSON, either registering Json or any subclass or factory building a suitable adapter." Is that right?cur.execute("INSERT INTO product(store_id, url, price, charecteristics, color, dimensions) VALUES (%d, %s, %s, %d, %s, %s)", (1, 'http://www.google.com', '$20', thedictionary, 'red', '8.5x11')) psycopg2.ProgrammingError: can't adapt type 'dict'
I'm not sure how to perform this precise thing, as I'm new to psycopg2.