【发布时间】:2015-08-03 20:37:03
【问题描述】:
我想将 python 字典作为 json 插入我的 postgresql 数据库(通过 python 和 psycopg2)。
我有:
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'))
它给出了错误信息:
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'
我不知道如何从这里开始。 我在互联网上找不到任何关于如何做这种确切的事情的信息,而且我对 psycopg2 很陌生。
【问题讨论】:
-
@bibangamba 是使用最新 postgres 版本的人应该查看的链接吗?
-
我觉得没问题。我正在使用 postgres 10,它没有任何问题
-
@Felipe Augusto 有最好的答案,谢谢!
标签: python postgresql dictionary psycopg2