首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在MySQL数据库中包含单引号

在MySQL数据库中包含单引号
EN

Stack Overflow用户
提问于 2018-10-29 05:15:59
回答 2查看 1.7K关注 0票数 1

MySQL表中有一个JSON类型。但是,如果数据值包含一个单引号,则它将失败,从而导致SQL语法错误。请查找SQL查询:

如果在单引号前添加转义字符,则可以解决此问题。但是,在MYSQL中是否有任何易于使用的JSON函数?我们必须从另一个用Java编写的应用程序中提供查询,如果可能的话,我们不想修改程序。

代码语言:javascript
复制
INSERT INTO integration.staging_correspondence_inbound_invoice (correlation_id,interface_name,event_status,event_context,created_by,created_date) values ('1540626495812-0-1','invoice','EXCEPTION_CAUGHT',JSON_ARRAY('[ {
  "exceptionRootCauseMessage" : "UpdateFailedException: Unable to update correspondence for correspondence id '68000'.Exception:{null\n> Record set for Correspondence with ids 68000 contains no records.\n>> }",
  "inExchangeBody" : null,
  "exceptionMessage" : "UpdateFailedException: Unable to update correspondence for correspondence id '68000'.Exception:{null\n> Record set for Correspondence with ids 68000 contains no records.\n>> }"
}, { }, {
  "EXCEPTION_CAUGHT" : "Unable to update correspondence for correspondence id '68000'.Exception:{null\n> Record set for Correspondence with ids 68000 contains no records.\n>> }"
} ]'),'Integration_User',NOW())

我们使用的是MySQL 5.7。在上面的查询中,"event_context"是JSON类型字段。

EN

回答 2

Stack Overflow用户

发布于 2018-10-29 05:37:26

您可以查看MySql 报价函数。它应该能解决你的问题。根据文件:

-> Quote ->(-> str --> )->

该函数通过用单引号包围字符串,并在每个单引号、反斜杠、ASCII和control前面加上一个反斜杠来实现这一点。

票数 0
EN

Stack Overflow用户

发布于 2021-11-28 04:17:12

使用base64

代码语言:javascript
复制
<?php
    $json = base64_encode($json);
    $sql = "UPDATE `entries` e SET e.fields = FROM_BASE64('".$json."') WHERE form_id = 163 AND e.fields LIKE '%$test_id%' LIMIT 1";
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53039121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档