PostgreSQL INSERT 多个 JSONB 列用于记录
PostgreSQL INSERT multiple JSONB columns for record
我无法使用 python 和 pyscopg2 将多个 python dict 插入单独的 JSONB 列中以获得一个 cur.execute 的新记录(自动 ID)。我可以单独插入每个字典,但没有指定 ID,它会创建新记录。
创建字典和Table:
dict1 = {"dict1Key1":"dict1value1", "dict1Key2":"dict1value2"}
dict2 = {"dict2Key1":"dict2value1", "dict2Key2":"dict2value2"}
query_test_jsonb_many = """CREATE TABLE IF NOT EXISTS table_test_jsonb_many (id serial PRIMARY KEY NOT NULL, col1_jsonb JSONB, col2_jsonb JSONB);"""
try:
with psycopg2.connect(dbname=dbname, user=user,
password=password, port=port, host=host) as con:
cur = con.cursor()
cur.execute(query_test_jsonb_many)
print(query_test_jsonb_many)
con.commit()
except psycopg2.Error as e:
print("Fail to execute due to the error:", e)
print("==============")
print("dict1: " + str(dict1))
print("dict1 Type is: " + str(type(dict1)))
print("==============")
print("dict2: " + str(dict2))
print("dict1 Type is: " + str(type(dict1)))
print("==============")
成功分别加载 dict1 > col1_jsonb 和 dict2 > col2_jsonb
try:
with psycopg2.connect(dbname=dbname, user=user,
password=password, port=port, host=host) as con:
cur = con.cursor()
cur.execute(f'''INSERT INTO table_test_jsonb_many (col1_jsonb) VALUES (%s::jsonb);''',([json.dumps(dict(dict1))]))
cur.execute(f'''INSERT INTO table_test_jsonb_many (col2_jsonb) VALUES (%s::jsonb);''',([json.dumps(dict(dict2))]))
except psycopg2.Error as e:
print("Fail to execute due to the error:", e)
无法在一个查询执行命令中加载 dict1 > col1_jsonb 和 dict2 > col2_jsonb
try:
with psycopg2.connect(dbname=dbname, user=user,
password=password, port=port, host=host) as con:
cur = con.cursor()
cur.execute(f'''INSERT INTO table_test_jsonb_many (col1_jsonb, col2_jsonb) VALUES (%s::jsonb, %s::jsonb);''',([json.dumps(dict(dict1))],[json.dumps(dict(dict2))]))
except psycopg2.Error as e:
print("Fail to execute due to the error:", e)
screenshot of pgadmin query
使用 JSON adaption 内置的 psycopg2
并执行:
import psycopg2
from psycopg2.extras import Json
cur.execute("INSERT INTO table_test_jsonb_many (col1_jsonb, col2_jsonb) VALUES (%s, %s)",[Json(dict1), Json(dict2)])
备注:
不要使用 F
字符串,在这种情况下也不需要。
不需要%s::jsonb
,类型适配由psycopg2
处理
使用 psycopg2
JSON 适配器 Json
来正确调整字典。
我无法使用 python 和 pyscopg2 将多个 python dict 插入单独的 JSONB 列中以获得一个 cur.execute 的新记录(自动 ID)。我可以单独插入每个字典,但没有指定 ID,它会创建新记录。
创建字典和Table:
dict1 = {"dict1Key1":"dict1value1", "dict1Key2":"dict1value2"}
dict2 = {"dict2Key1":"dict2value1", "dict2Key2":"dict2value2"}
query_test_jsonb_many = """CREATE TABLE IF NOT EXISTS table_test_jsonb_many (id serial PRIMARY KEY NOT NULL, col1_jsonb JSONB, col2_jsonb JSONB);"""
try:
with psycopg2.connect(dbname=dbname, user=user,
password=password, port=port, host=host) as con:
cur = con.cursor()
cur.execute(query_test_jsonb_many)
print(query_test_jsonb_many)
con.commit()
except psycopg2.Error as e:
print("Fail to execute due to the error:", e)
print("==============")
print("dict1: " + str(dict1))
print("dict1 Type is: " + str(type(dict1)))
print("==============")
print("dict2: " + str(dict2))
print("dict1 Type is: " + str(type(dict1)))
print("==============")
成功分别加载 dict1 > col1_jsonb 和 dict2 > col2_jsonb
try:
with psycopg2.connect(dbname=dbname, user=user,
password=password, port=port, host=host) as con:
cur = con.cursor()
cur.execute(f'''INSERT INTO table_test_jsonb_many (col1_jsonb) VALUES (%s::jsonb);''',([json.dumps(dict(dict1))]))
cur.execute(f'''INSERT INTO table_test_jsonb_many (col2_jsonb) VALUES (%s::jsonb);''',([json.dumps(dict(dict2))]))
except psycopg2.Error as e:
print("Fail to execute due to the error:", e)
无法在一个查询执行命令中加载 dict1 > col1_jsonb 和 dict2 > col2_jsonb
try:
with psycopg2.connect(dbname=dbname, user=user,
password=password, port=port, host=host) as con:
cur = con.cursor()
cur.execute(f'''INSERT INTO table_test_jsonb_many (col1_jsonb, col2_jsonb) VALUES (%s::jsonb, %s::jsonb);''',([json.dumps(dict(dict1))],[json.dumps(dict(dict2))]))
except psycopg2.Error as e:
print("Fail to execute due to the error:", e)
screenshot of pgadmin query
使用 JSON adaption 内置的 psycopg2
并执行:
import psycopg2
from psycopg2.extras import Json
cur.execute("INSERT INTO table_test_jsonb_many (col1_jsonb, col2_jsonb) VALUES (%s, %s)",[Json(dict1), Json(dict2)])
备注:
不要使用
F
字符串,在这种情况下也不需要。不需要
处理%s::jsonb
,类型适配由psycopg2
使用
psycopg2
JSON 适配器Json
来正确调整字典。