首页 > 解决方案 > 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 类型字段。

标签: mysqlsqldatabase

解决方案


使用 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";
?>

推荐阅读