我想使用 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/