【问题标题】:How to insert POINT type value using PreparedStatement from a java app into Postgresql DB如何使用 PreparedStatement 从 Java 应用程序将 POINT 类型值插入 Postgresql DB
【发布时间】:2013-09-08 13:33:55
【问题描述】:

以下查询在 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 值),但还没有解决这个问题。提前感谢帮助。

【问题讨论】:

    标签: java sql postgresql insert point


    【解决方案1】:

    你可以轻松制作:

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

    【讨论】:

      【解决方案2】:

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

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

      对于每个 POINT 列。

      【讨论】:

        猜你喜欢
        • 2017-04-04
        • 2021-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-18
        相关资源
        最近更新 更多