2

以下查询适用于 pgAdmin 的 sql 控制台:

insert into sometable values (DEFAULT,9, 'test content', '(-194.0, 53.0)', '(+144.345345, -453.043534)', '2012-08-24 14:00:00 +02:00', '2012-08-24 14:00:00 +02:00');

以下是发送到服务器的值:

nameValuePair.add(new BasicNameValuePair("userid", "9"));
nameValuePair.add(new BasicNameValuePair("content", "test content"));
nameValuePair.add(new BasicNameValuePair("location1", "(-194.0, 53.0)"));
nameValuePair.add(new BasicNameValuePair("location2", "(+134.350, -433.04345)"));
nameValuePair.add(new BasicNameValuePair("date1", "2012-08-24 14:00:00 +02:00"));
nameValuePair.add(new BasicNameValuePair("date2", "2012-08-24 14:00:00 +02:00"));

PreparedStatement(在服务器上)

psInsert = conn.prepareStatement("insert into OFFERS (USERID, CONTENT, LOCATION1, LOCATION2, DATE1, DATE2) values (?, ?, ?, ?, ?, ?)");

psInsert.setInt(1, userid);
psInsert.setString(2, content);
psInsert.setString(3, location1);
psInsert.setString(4, location);
psInsert.setString(5, date1);
psInsert.setString(6, date2);

psInsert.executeUpdate();

这会导致以下错误:

org.postgresql.util.PSQLException: ERROR: column "location1" is of type point but expression is of type character varying
**strong text**Hint: You will need to rewrite or cast the expression.

我已经阅读了一些其他相关的帖子(关于如何在 Postgresql db 中插入 GeoSpatial/Point 值),但还没有解决这个问题。提前感谢帮助。

4

2 回答 2

3

您可以轻松制作:

psInsert = conn.prepareStatement(
"insert into OFFERS (USERID, CONTENT, LOCATION1, LOCATION2, DATE1, DATE2) " + 
"values (?, ?, point(?, ?), point(?, ?), ?, ?)" );
于 2013-12-13T16:37:28.013 回答
2

我会传递实际xy坐标(或解析服务器上的字符串)并使用ST_MakePoint

psInsert = conn.prepareStatement(
    "insert into OFFERS (USERID, CONTENT, LOCATION1, LOCATION2, DATE1, DATE2) " + 
    "values (?, ?, ST_MakePoint(?, ?), ST_MakePoint(?, ?), ?, ?)"
);

对于每一POINT列。

于 2013-09-08T13:51:08.137 回答