Skip to content
Advertisement

Insert small dictionary like {1:23, 2:45, 3:17} into a column of SQL database table in Postgres using python language

I have a table having one varchar type column and 2 json type columns, which i created using:

I now want to insert values like this using python:

For which i wrote a string in python like this to execute:

But this doesn’t work. I am getting following error:

I searched for similar answers but couln’t find one. This should be simple.

It should look like this in the table:

Advertisement

Answer

Simply use json.dumps for json data (serializes to string) as mentioned in the docs and let psycopg2 do all work and parameter binding:

Out:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement