Updating a JSON-like column in Postgres with Python and Psychopg2

Asked

Viewed 45 times

0

When trying to run an update like the below in postgres via psycopg2 of Python is giving the error:

psycopg2.Programmingerror: can’t Adapt type 'Dict'

I’m trying to run a code like this:

self.cursor.execute("UPDATE tb_games set infos_json = %s where id = %s", (json.dumps({'v1':'a','v2:'b'}), id_game))
conexao.commit()

My column infos_json is the type json, the dictionary I’m passing to json.dumps is a valid dictionary, already I have JSON on code parsers and all right.

1 answer

0


If this snippet of code you posted is the same as in the program, there is an error in this part of the code

json.dumps({'v1':'a','v2:'b'}), id_game)
                        ^ deveria ter um ' aqui`

try replacing with

self.cursor.execute("UPDATE tb_games set infos_json = %s where id = %s", (json.dumps({'v1':'a','v2':'b'}), id_game))
conexao.commit()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.