【问题标题】:Storing JSON In MySQL [duplicate]在 MySQL 中存储 JSON [重复]
【发布时间】:2018-05-14 02:16:42
【问题描述】:

我正在尝试将 JSON 作为字符串存储在 mySql 中。我收到以下错误:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key, value) values ('SYSTEM_USER', '2017-11-30 13:28:38.512', 'SYSTEM_US' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)

代码片段:

    final Information informationObj = Information.newBuilder().withCreateBy("").withCreatedDate(null)
    .withModifiedBy("").withModifiedDate(null).withValue(jsonString).withKey(id).build();
    dataRepository.save(informationObj);

Value 是 db 中的 json 类型。表中的字段:

`_id`, `key`, `value`, `created_date`, `created_by`, `modified_by`, `modified_date`

我认为,可能是因为 json 格式,因为它包含逗号等字符。将此 json 存储在 MYSQL DB 中的解决方案是什么。 谢谢,

【问题讨论】:

  • 希望对你有所帮助dev.mysql.com/doc/refman/5.7/en/json.html有一个json数据类型
  • 错误在您的代码中。但是我们无法知道您的代码是什么样的,所以...
  • 直觉:一种 SQL 注入。显示代码使用参数
  • @JBNizet 使用代码 sn-p 更新。

标签: java mysql spring jpa jdbc


【解决方案1】:

此异常发生在您尝试使用破坏 sql 插入的受限关键字时。

System_user 可能是 MySQL 中的一个关键词

你在使用 jpa 插入吗?

【讨论】:

  • 是的,JPA 插入
  • 那么问题与musql保留关键字有关。
  • 是的,这是保留关键字“key”的问题
猜你喜欢
  • 1970-01-01
  • 2021-05-17
  • 2013-03-01
  • 2012-05-06
  • 1970-01-01
  • 2016-04-29
  • 2018-03-26
  • 1970-01-01
  • 2012-08-07
相关资源
最近更新 更多