mysql - JSON 字符串 - 在 MySQL 数据库中包含单引号

标签 mysql sql database

我在 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类型字段。

最佳答案

你可以看看MySql Quote功能。它应该可以解决您的问题。 根据文档:

--> 引用 --> ( --> str --> ) -->

The function achieves this by enclosing the string with single quotes, and by preceding each single quote, backslash, ASCII NUL and control-Z with a backslash.

关于mysql - JSON 字符串 - 在 MySQL 数据库中包含单引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53039121/

相关文章:

mysql - 寻找 SQL 查询的替代方案

sql - 想要从显示内容中排除数字

java - 在没有服务器的情况下连接到 Intellij Ultimate 中的本地 MySQL 数据库?

MySQL_如何对另一列上具有相同值的每两列中的值求和?

php - 如何向数据库中插入一些与另一行相同的信息?

Python,如何采用逗号分隔的 sys argv,并将其设置为 '1' ,'2' (对于 MySQL "IN"查询)

MySQL:使用一个查询从第二个表返回行数

php - 使用 mySQLi 保留字和函数 - 安全

PHP PDO - 带 WHERE 的 MySQL INSERT

mysql - SQL 获取与所有键相关的所有结果