mysql - JSON 字符串 - 在 MySQL 数据库中包含单引号
问题描述
MySQL
我在表格中有一个 JSON 类型。但是,如果数据值包含单引号,则说明 SQL 语法错误。请找到 SQL 查询:
如果我们在单引号之前添加转义字符,我们可以解决这个问题。但是 MYSQL 中是否有任何可用的 JSON 函数可以轻松使用?我们必须从另一个用 Java 编写的应用程序提供查询,如果可能,我们不想修改程序。
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 类型字段。
解决方案
使用 base64
<?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";
?>
推荐阅读
- amazon-dynamodb - ConditionExpression in DynamoDB update operation gives unknown error
- javascript - 如何使整个网页在阅读模式下变成淡黄色?
- reactjs - 功能组件到类组件
- python - 如何检查文本大小是否超过 tkinter 文本小部件大小
- javascript - 消息应用程序:跨越添加而不是创建新行
- javascript - FindIndex 结果为真,为什么它返回 -1
- swift - swift / Xcode标签的随机播放顺序
- c# - 从另一个txt列表中调用一个txt列表c#
- c++ - 试图通过 qserialport 将 arduino 与 pc 通信
- html - 尝试使用 BEM 方法,我在正确的轨道上吗?