【问题标题】:UPDATE existing row to insert an array of json - postgresql更新现有行以插入 json 数组 - postgresql
【发布时间】: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


    【解决方案1】:

    找到了!

    我的代码中有一个愚蠢的错误,忘记引用WHERE ... AND 语句的值。

    cur.execute("""UPDATE company_officers_list 
                    SET 
                        former_names = (%s::json[]) 
                    WHERE 
                        comp_code = '{}'
                    AND
                        name = '{}'
                 """.format(officer_item_copy["comp_code"], officer_item_copy["name"]), 
               insert_data)
    

    另一个可能的解决方案是使用array_cat 函数,因为我正在插入一个空字段。

    cur.execute("""UPDATE company_officers_list 
    
                   SET former_names = array_cat(former_names, (%s::json[])) 
    
                   WHERE comp_code = '{}' 
    
                   AND name = '{}';""".format(officer_item_copy["comp_code"],
                                              officer_item_copy["name"]), 
    
                insert_data)
    

    此链接帮助我查看了array_cat 函数的示例:

    https://makandracards.com/makandra/36097-postgresql-how-to-add-remove-modify-array-values-and-how-to-replace-1-value-with-multiple-values

    一般更新页面文档页面帮助我发现了缺失的''

    https://www.postgresql.org/docs/9.1/sql-update.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-16
      • 1970-01-01
      • 2014-01-15
      • 2016-02-23
      • 2022-11-11
      • 1970-01-01
      • 2015-08-21
      • 2022-01-26
      相关资源
      最近更新 更多