将 Python 个字典列表转换为 json 个 Postgresql 数组
Convert Python list of dicts into Postgresql array of json
我正在尝试将 jsonb 元素的 Python(2.7) 列表插入到 Postgresql(9.4) table 中,数据类型列为:jsonb[].
这是一些代码:
import json
anArray = [{"name":"Joe","age":51,"yob":1964,"gender":"male"},{"name":"George","age":41,"dob":1974,"gender":"male"},{"name":"Nick","age":31,"dob":1984,"gender":"male"}]
myArray = []
#here's what I have so far:
for e in anArray:
myArray.append(json.dumps(e))
#this gives me
myArray = ['{"name":"Joe","age":51,"yob":1964,"gender":"male"}','{"name":"George","age":41,"dob":1974,"gender":"male"}','{"name":"Nick","age":31,"dob":1984,"gender":"male"}']
#insert commands
insert_sql = "INSERT INTO my_table (data) VALUES (%s);"
insert_data = (myArray, )
cursor.execute(insert_sql, insert_data)
现在当我尝试插入 myArray 时,psycopg2 给我一个错误
psycopg2.ProgrammingError: column "data" is of type jsonb[] but expression is of type text[]
我不太确定将此数据插入 table 的正确语法是什么。任何 help/pointers 将不胜感激。
解决方案
感谢 piro,这是一个快速的解决方案。
insert_sql = "INSERT INTO my_table (columns) VALUES (%s::jsonb[]);"
insert_data = (myArray, )
cursor.execute(insert_sql, insert_data)
这不是 psycopg 错误:这是 psycopg 所依赖的 PostgreSQL 错误。
错误似乎暗示没有隐式文本[]->jsonb[] 转换,因此您需要手动添加一个:
INSERT INTO my_table (columns) VALUES (%s::jsonb[]);
from psycopg2.extras import Json
data = [
{"name":"Joe","age":51,"yob":1964,"gender":"male"},
{"name":"George","age":41,"dob":1974,"gender":"male"},
{"name":"Nick","age":31,"dob":1984,"gender":"male"}
]
query = '''
insert into t (j)
select array_agg(j)
from jsonb_array_elements(%s) s(j)
'''
cursor.execute(query, (Json(data),))
jsonb_array_elements
将 return 一组 jsonb
将由 array_agg
变成一个 jsonb
的数组
我正在尝试将 jsonb 元素的 Python(2.7) 列表插入到 Postgresql(9.4) table 中,数据类型列为:jsonb[].
这是一些代码:
import json
anArray = [{"name":"Joe","age":51,"yob":1964,"gender":"male"},{"name":"George","age":41,"dob":1974,"gender":"male"},{"name":"Nick","age":31,"dob":1984,"gender":"male"}]
myArray = []
#here's what I have so far:
for e in anArray:
myArray.append(json.dumps(e))
#this gives me
myArray = ['{"name":"Joe","age":51,"yob":1964,"gender":"male"}','{"name":"George","age":41,"dob":1974,"gender":"male"}','{"name":"Nick","age":31,"dob":1984,"gender":"male"}']
#insert commands
insert_sql = "INSERT INTO my_table (data) VALUES (%s);"
insert_data = (myArray, )
cursor.execute(insert_sql, insert_data)
现在当我尝试插入 myArray 时,psycopg2 给我一个错误
psycopg2.ProgrammingError: column "data" is of type jsonb[] but expression is of type text[]
我不太确定将此数据插入 table 的正确语法是什么。任何 help/pointers 将不胜感激。
解决方案 感谢 piro,这是一个快速的解决方案。
insert_sql = "INSERT INTO my_table (columns) VALUES (%s::jsonb[]);"
insert_data = (myArray, )
cursor.execute(insert_sql, insert_data)
这不是 psycopg 错误:这是 psycopg 所依赖的 PostgreSQL 错误。
错误似乎暗示没有隐式文本[]->jsonb[] 转换,因此您需要手动添加一个:
INSERT INTO my_table (columns) VALUES (%s::jsonb[]);
from psycopg2.extras import Json
data = [
{"name":"Joe","age":51,"yob":1964,"gender":"male"},
{"name":"George","age":41,"dob":1974,"gender":"male"},
{"name":"Nick","age":31,"dob":1984,"gender":"male"}
]
query = '''
insert into t (j)
select array_agg(j)
from jsonb_array_elements(%s) s(j)
'''
cursor.execute(query, (Json(data),))
jsonb_array_elements
将 return 一组 jsonb
将由 array_agg
jsonb
的数组