【发布时间】:2017-06-22 06:11:51
【问题描述】:
我目前正在尝试使用 psycopg2 将两个 json 对象列表存储到我的 postgresql 数据库中,但没有成功。
这是我正在尝试做的事情的 sn-p...
SQL_INSERT_QUERY = """INSERT INTO table (
x,
json_list_one,
json_list_two
) VALUES (%s, %s, %s)"""
def insert_stuff(id, json_list_one, json_list_two):
values = (
id,
map(psycopg2.extras.Json, json_list_one),
map(psycopg2.extras.Json, json_list_two)
)
conn = get_connection_pool().getconn()
try:
cur = conn.cursor()
cur.execute(SQL_INSERT_QUERY, values)
conn.commit()
cur.close()
finally:
get_connection_pool().putconn(conn)
当前的实现导致了这个错误...
An exception of type ProgrammingError occured. Arguments:
('column "json_list_one" is of type json[] but expression is of type text[]\nLINE 5: ...) VALUES (\'9527d790-31dc-46fa-b683-c6fc9807c2a4\', ARRAY[\'{"h...\n ^\nHINT: You will need to rewrite or cast the expression.\n',)
有人知道我应该如何在 PostgreSQL 中插入一个 json 对象数组吗?
谢谢。
【问题讨论】:
-
stackoverflow.com/a/45150668/3598837 这个答案为我解决了
标签: python arrays json postgresql psycopg2