以下の内容はhttps://www.yokoyan.net/entry/2018/05/11/130000より取得しました。


psycopg2.ProgrammingError: can't adapt type 'dict'が発生したときの対処法 #Python3 #PostgreSQL9.6

やりたいこと

Pythonを使って、JSONデータをPostgreSQLにINSERTしたい

実行環境

  • PostgreSQL9.6.6
  • Python3.6
  • psycopg2.7.4

発生したエラー

psycopg2.ProgrammingError: can't adapt type 'dict'

解決法

JSONデータが格納されている変数に対して、json.dumps()を使ってエンコードする。

import json

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 (%s, %s, %s, %s, %s, %s)", (1,  'http://www.google.com', '$20', json.dumps(thedictionary), 'red', '8.5x11'))

参考情報




以上の内容はhttps://www.yokoyan.net/entry/2018/05/11/130000より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14