postgresql - 执行 COPY..TO 语句时无法确定参数 $1 的数据类型

标签 postgresql jdbc

我想使用 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 语句一起使用的原因是什么?

最佳答案

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

关于postgresql - 执行 COPY..TO 语句时无法确定参数 $1 的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39341810/

相关文章:

Postgresql 列出外部数据包装器?

.net - Advantage Database Server .net 提供程序表未显示

java - 直接文件路径不起作用

java - 如何验证表中某列是否存在

java - 连接到远程 postgresql 数据库时连接超时

sql - 通过postgresql计算正确率

postgresql - Postgres Varchar 比较文档?

postgresql - 如何使普通 linux 用户无需 sudo 访问权限即可访问 postgres 数据库?

laravel - SQLSTATE[42883] : Undefined function: 7 ERROR: function date_format(date, 未知)不存在

java - 将 DB2 许可证集成到独立 JAR 应用程序中