【问题标题】:Update the mysql table row of column type JsonBlob更新列类型JsonBlob的mysql表行
【发布时间】:2016-02-03 03:27:27
【问题描述】:

考虑表字段'extra'的结构如下:

extra = sql.Column(sql.JsonBlob())

所以它是 JsonBlob 类型的。

现在我需要在“额外”字段中更新一些值。

当我尝试更新 'extra' 值时,最终会出错,如下所示:

  File "basic_test.py", line 38
    sql = "update project set extra=('{"creation_date": "%s"}') where id=('%s')" % (date, tenant_id)
                                                    ^
SyntaxError: invalid syntax

我试过的代码如下:

    import datetime
    mylist = []
    today = datetime.date.today()
    mylist.append(today)
    date = mylist[0]

    tenant_id = '1578f81703ec4bbaa1d548532c922ab9'

    sql = "update project set extra='{"creation_date": "%s"}' where id=('%s')" % (date, tenant_id)
    cur.execute(sql)

注意:

例如,我在这里提供了与另一个表中使用的相同类型的数据。

我已从表中选择行并在此处提供示例。

('{"email": "test@example.com"}',)
<type 'tuple'>

所以这是存储在额外字段中的数据类型。

有人看看,让我知道你的cmets。

如果需要更多信息,请告诉我。

提前致谢。

【问题讨论】:

    标签: python mysql json


    【解决方案1】:

    我自己找到了答案。

    extra = '{"creation_date": "%s"}' % date
    
    sql = "update project set extra=('%s') where id=('%s')" % (extra, tenant_id)
    

    如上所述更改了查询,以便我可以看到数据库表正在根据需要正确更新。

    【讨论】:

      猜你喜欢
      • 2021-06-16
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 2014-12-03
      • 2015-06-28
      • 2014-04-01
      相关资源
      最近更新 更多