【发布时间】:2013-09-22 01:30:18
【问题描述】:
我似乎无法在准备好的语句中设置正确的类型。这段代码:
String sql = "delete from foo where ctid = ?";
PreparedStatement deleteStmt = conn.prepareStatement( sql );
deleteStmt.setString(1, "(0,43)"); // select ctid from foo shows (0,43) exists....
int a = deleteStmt.executeUpdate();
抛出此异常:
org.postgresql.util.PSQLException: ERROR: operator does not exist: tid = character varying
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts. Position: 28
请注意,在 psql 中,删除操作使用字符串:
mydb=# DELETE FROM foo where ctid = '(0,43)';
DELETE 1
JDBC PreparedStatement 中 tid 的正确类型/编码是什么?我试过 setRowId() (抛出 ava.sql.SQLFeatureNotSupportedException:方法 org.postgresql.jdbc4.Jdbc4PreparedStatement.setRowId(int, RowId) 尚未实现。)和 setBytes() (抛出 ...操作符不存在:tid = 字节)
【问题讨论】:
-
如果您尝试使用
deleteStmt.setObject(1, "(0,43)");会怎样?它会抛出什么异常? -
另一种可能的解决方案:
setString(1, "(0,43)::ctid")或者不使用 PreparedStatement 并使用动态 SQL。 -
setObject(1, "(0,43)") 和 setString(1, "(0,43)::ctid") 都抛出 org.postgresql.util.PSQLException: ERROR: operator does不存在:tid = 字符变化。在 PreparedStatement 上没有替换的动态 SQL(例如 sql = "delete from foo where ctid = '(0,50)'"; deleteStmt = conn.prepareStatement( sql ); ) 确实有效。
-
否则 - 如果可能的话 - 将您的数据类型更改为 varchar
-
ctid 是 tid 类型的内部列,不能更改。
标签: java postgresql jdbc prepared-statement