MySQL insert/update 包含使用准备语句的点类型的行

MySQL insert/update a row containing a point type using a prepared statement

我正在使用准备好的语句尝试将新行插入 MySQL table,如果索引存在,则执行更新。我相当确定该问题与标记为 "grp_hall_geoPoint" 的 "point" 字段有关。我收到错误:

"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"

传递的值是:

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

准备好的语句是:

String insertUpdateQry = ("INSERT INTO at_group " +
                    "(grp_country, grp_state, grp_region, grp_district, grp_name, " +
                        " grp_section, grp_sub_section, grp_archived, grp_hall_geoPoint, grp_nas) " +
                    "VALUES (?, ?, ?, ?, ?, ?, ?, ?, PointFromText(CONCAT('POINT(', ?, ' ', ?, ')')), ?)" +
                    "ON DUPLICATE KEY " +
                    "UPDATE at_group " +
                      "SET grp_country=?, grp_state=?, grp_region=?, grp_district=?, " +
                      "grp_name=?, grp_section=?, grp_sub_section=?, grp_archived=?, " +
                      "grp_hall_geoPoint=PointFromText(CONCAT('POINT(', ?, ' ', ?, ')')), grp_nas=?;");

            try {
                // Get Connection and Statement from DataSource
                c = ds.getConnection();
                ps = c.prepareStatement(insertUpdateQry);

                try {
                    // Create a statement and execute the query on it
                    ps.setString(1, "Australia");
                    ps.setString(2, state);
                    ps.setString(3, region);
                    ps.setString(4, district);
                    ps.setString(5, group);
                    ps.setString(6, section);
                    ps.setString(7, subSection);
                    ps.setString(8, archiveDate);
                    ps.setString(9, latitude);
                    ps.setString(10, longditude);
                    ps.setString(11, nas);

                    ps.setString(12, "Australia");
                    ps.setString(13, state);
                    ps.setString(14, region);
                    ps.setString(15, district);
                    ps.setString(16, group);
                    ps.setString(17, section);
                    ps.setString(18, subSection);
                    ps.setString(19, archiveDate);
                    ps.setString(20, latitude);
                    ps.setString(21, longditude);
                    ps.setString(22, nas);

                    ps.executeUpdate();

以下在唯一索引中:grp_country、grp_state、grp_region、grp_district、grp_name、grp_section、grp_sub_section

grp_hall_geoPoint 是类型点

我已经使用测试数据触发更新和插入,结果相同。

语法错误与点数据类型无关。您不得在 on duplicate key update 子句中的 update 之后提供 table 名称和 set

所以,而不是

...
"ON DUPLICATE KEY " +
"UPDATE at_group " +
"SET grp_country=?, ...

你只需要写:

...
"ON DUPLICATE KEY " +
"UPDATE grp_country=?, ...

您可以将此查询更改为(已删除 table 名称和设置语句):

此处参考: http://www.mysqltutorial.org/mysql-insert-or-update-on-duplicate-key-update/

String insertUpdateQry = ("INSERT INTO at_group " +
                    "(grp_country, grp_state, grp_region, grp_district, grp_group, " +
                        " grp_section, grp_sub_section, grp_archived, grp_hall_geoPoint, grp_nas) " +
                    "VALUES (?, ?, ?, ?, ?, ?, ?, ?, PointFromText(CONCAT('POINT(', ?, ' ', ?, ')')), ?)" +
                    "ON DUPLICATE KEY " +
                    "UPDATE " +
                      "grp_country=?, grp_state=?, grp_region=?, grp_district=?, " +
                      "grp_name=?, grp_section=?, grp_sub_section=?, grp_archived=?, " +
                      "grp_hall_geoPoint=PointFromText(CONCAT('POINT(', ?, ' ', ?, ')')), grp_nas=?;");