【发布时间】:2019-04-08 15:24:30
【问题描述】:
我的表 a_table 带有这个主键:
PRIMARY KEY (comp_code, name)。
我从名为 officer_item_copy 的字典中插入数据:
{
'address': 'string',
'name' : 'a non unique name'
'appointed_on': '23/2/1988',
'comp_code': 'OC319508',
'former_names': [
{'forenames': 'string',
'surname': 'string'},
{'forenames': 'string', 'surname': 'string'}
],
}
在former_names 列中,我想存储有关以前名称的信息,这是 Python 中的字典列表,因此我创建了以下限制 JSON [],我想在其中插入某个 json 数组。
感谢this post,我了解如何构造一个插入语句 - 但这会插入一个新行,而我想将 json 数组插入到具有某个主键(名称、代码)的现有数组中。
我使用this post 作为下面代码的参考。
到目前为止,这是我的代码:
# cast dictionaries to json objects
json_array = [json.dumps(i) for i in list_of_dicts_former_names]
insert_data = (json_array, )
# update table "a_table"
cur.execute("""UPDATE company_officers_list
SET
former_names = (%s::json[]),
WHERE
comp_code = {1}
AND
name ={2}""".format(officer_item_copy["comp_code"],
officer_item_copy["name"]),
insert_data)
我的错误:
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
<ipython-input-472-af4c715da33d> in <module>
7 name = {}
8 """.format(officer_item_copy["comp_code"], officer_item_copy["name"]),
----> 9 insert_data)
ProgrammingError: syntax error at or near "Name"
LINE 7: name = Unique Name 2
【问题讨论】:
标签: python sql json postgresql psycopg2