【问题标题】:could not determine data type of parameter $1, while executing COPY..TO statement在执行 COPY..TO 语句时无法确定参数 $1 的数据类型
【发布时间】:2016-09-06 06:17:09
【问题描述】:

我想使用 JDBC 准备语句将数据库中的表内容写入 CSV 文件。我使用的 PSQL 查询是:

COPY(select * from file where uploaded_at > ?) TO '/tmp/file_info.csv' With DELIMITER ',' CSV HEADER ";

我的代码如下:

private void copyData(Connection conn, Date date){
String sql = "COPY (select * from file where uploaded_at< ?) TO '/tmp/file_info.csv' With DELIMITER ',' CSV HEADER ";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setTimestamp(1, new Timestamp(date.getTime()));
stmt.execute();
}

运行此查询时,我得到以下堆栈:

org.postgresql.util.PSQLException: ERROR: could not determine data type of parameter $1
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:381)
    at tests.com.paramatrix.pace.archival.file.TestFileArchival.main(TestFileArchival.java:63)

运行简单的选择查询时不会生成此异常,例如:

select * from file where uploaded_at > ?

它不能与 COPY..TO 语句一起使用的原因可能是什么?

【问题讨论】:

    标签: postgresql jdbc


    【解决方案1】:

    您不能参数化COPY 语句。您必须使用字符串插值或 PgJDBC 的 CopyManager

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-28
      • 2018-02-19
      • 2017-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多