mysql使用准备好的语句插入/更新包含点类型的行

pkmbmrz7  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(435)

我正在使用一个prepared语句尝试向mysql表中插入新行,如果索引存在,则执行更新。我相当肯定,这个问题是有关的“点”字段标记为“grp\U霍尔\U地球点”。我收到错误:

  1. "SQLException in updateGroup: 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 'SET grp_country='Australia', grp_state='New South Wales', grp_region='Sydney Nor' at line 1"

传递的值是:

  1. public static void updateGroup(String state,
  2. String region, String district, String group, String section,
  3. String subSection, String archiveDate, String latitude, String longditude, String nas) {[/code]

准备好的声明是:

  1. String insertUpdateQry = ("INSERT INTO at_group " +
  2. "(grp_country, grp_state, grp_region, grp_district, grp_name, " +
  3. " grp_section, grp_sub_section, grp_archived, grp_hall_geoPoint, grp_nas) " +
  4. "VALUES (?, ?, ?, ?, ?, ?, ?, ?, PointFromText(CONCAT('POINT(', ?, ' ', ?, ')')), ?)" +
  5. "ON DUPLICATE KEY " +
  6. "UPDATE at_group " +
  7. "SET grp_country=?, grp_state=?, grp_region=?, grp_district=?, " +
  8. "grp_name=?, grp_section=?, grp_sub_section=?, grp_archived=?, " +
  9. "grp_hall_geoPoint=PointFromText(CONCAT('POINT(', ?, ' ', ?, ')')), grp_nas=?;");
  10. try {
  11. // Get Connection and Statement from DataSource
  12. c = ds.getConnection();
  13. ps = c.prepareStatement(insertUpdateQry);
  14. try {
  15. // Create a statement and execute the query on it
  16. ps.setString(1, "Australia");
  17. ps.setString(2, state);
  18. ps.setString(3, region);
  19. ps.setString(4, district);
  20. ps.setString(5, group);
  21. ps.setString(6, section);
  22. ps.setString(7, subSection);
  23. ps.setString(8, archiveDate);
  24. ps.setString(9, latitude);
  25. ps.setString(10, longditude);
  26. ps.setString(11, nas);
  27. ps.setString(12, "Australia");
  28. ps.setString(13, state);
  29. ps.setString(14, region);
  30. ps.setString(15, district);
  31. ps.setString(16, group);
  32. ps.setString(17, section);
  33. ps.setString(18, subSection);
  34. ps.setString(19, archiveDate);
  35. ps.setString(20, latitude);
  36. ps.setString(21, longditude);
  37. ps.setString(22, nas);
  38. ps.executeUpdate();

以下是唯一索引:grp\u country、grp\u state、grp\u region、grp\u district、grp\u name、grp\u section、grp\u sub\u section
grp\ U hall\ U地质点是类型点
我使用测试数据触发更新和插入,结果相同。

7uzetpgm

7uzetpgm1#

您可以将此查询更改为(已删除表名和set语句):
此处引用:http://www.mysqltutorial.org/mysql-insert-or-update-on-duplicate-key-update/

  1. String insertUpdateQry = ("INSERT INTO at_group " +
  2. "(grp_country, grp_state, grp_region, grp_district, grp_group, " +
  3. " grp_section, grp_sub_section, grp_archived, grp_hall_geoPoint, grp_nas) " +
  4. "VALUES (?, ?, ?, ?, ?, ?, ?, ?, PointFromText(CONCAT('POINT(', ?, ' ', ?, ')')), ?)" +
  5. "ON DUPLICATE KEY " +
  6. "UPDATE " +
  7. "grp_country=?, grp_state=?, grp_region=?, grp_district=?, " +
  8. "grp_name=?, grp_section=?, grp_sub_section=?, grp_archived=?, " +
  9. "grp_hall_geoPoint=PointFromText(CONCAT('POINT(', ?, ' ', ?, ')')), grp_nas=?;");
hivapdat

hivapdat2#

语法错误与点数据类型无关。不能提供表名和 set 之后 updateon duplicate key update 条款。
所以

  1. ...
  2. "ON DUPLICATE KEY " +
  3. "UPDATE at_group " +
  4. "SET grp_country=?, ...

你只需写下:

  1. ...
  2. "ON DUPLICATE KEY " +
  3. "UPDATE grp_country=?, ...

相关问题