MySQL MBRContains statement throwing MySQLExceptionError

时间:2016-02-12 20:00:27

标签: java mysql jdbc spatial-query

I am working on a Java project, where we use its sql library functions to access and query from MySQL server.

The code that raises the problem is:

String setBox = "SET @box = 
                                'Polygon((" + lx + " " + ry + ","
                                            + rx + " " + ry + ","
                                            + lx + " " + ly + ","
                                            + rx + " " + ly + ","
                                            + lx + " " + ry + "))';\n";

ResultSet regionResult = stmt.executeQuery(setBox + 
  "SELECT ItemID FROM ItemPoint WHERE MBRContains(GeomFromText(@box), Coords);\n");

The error occurs in on the second statement (ResultSet regionResult = ...)

The error I receive is:

"com.mysql.jdbc.exception.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 user near 'SELECT ItemID FROM ItemPoint WHERE MBRContains((GeomFromText(@box), Coords)' at line 2"

I am not sure why there is a syntax error as I am basing it off: http://dev.mysql.com/doc/refman/5.5/en/using-spatial-indexes.html.

My MySQL server version is: 5.5.40-0ubuntu0.14.04.1 (Ubuntu)

2 个答案:

答案 0 :(得分:1)

You problem is not MySql is the if let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding) { print(responseString) self.lbl1.text = String(responseString) } . You cant use it to run two statements like that you have to use a PreparedStatement

For your problem it will be something like:

CallableStatement

Note that this may not work the way it is. You may need to change something, this is the basic idea. I'm doing this from memory, so...

答案 1 :(得分:1)

我能够通过这样做来实现它:

String box = "Polygon((" 
                        + lx + " " + ry + ", "
                        + rx + " " + ry + ", "
                        + rx + " " + ly + ", "
                        + lx + " " + ly + ", "
                        + lx + " " + ry + "))";

ResultSet regionResult = stmt.executeQuery(//setBox + 
  "SELECT ItemID FROM ItemPoint WHERE MBRContains(GeomFromText(' " + box +  " '), Coords);\n");