douzhang5121 2019-03-01 08:17
浏览 550

用于MySQL的GoLang查询附加到JSON字段

I'm trying to write a query that would append my JSON variable("data") to a JSON field in my database. I have a table called cart with three fields inside: id type int, status type varchar and items type json. So basically I'm trying to write a query where it would find my cart by the id and it would add an item to the end of my item field so far what I have is this:

query, err := db.Exec("UPDATE cart SET items = JSON_ARRAY_APPEND(@items, '$', 'data') where id = 1")

I know this is not enough. How can I add my variable instead of 'data' to this query? Could anyone help me?

Thank you.

  • 写回答

1条回答 默认 最新

  • douchuxun4162 2019-03-01 11:51
    关注

    My guess would be a parameterized update:

    db.Exec("UPDATE cart SET items = JSON_ARRAY_APPEND(@items, '$', ?) where id = ?", data, 1)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))