7

我似乎无法在准备好的语句中设置正确的类型。这段代码:

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 =字节)

4

2 回答 2

3

解决了!您必须手动创建 PGO 对象并设置类型和值并将其作为对象传递给 JDBC。这现在有效:

sql = "delete from foo where ctid = ?";
deleteStmt = conn.prepareStatement( sql );
org.postgresql.util.PGobject pgo = new org.postgresql.util.PGobject();
pgo.setType("tid");
pgo.setValue("(0,54)");  // value is a string as might be returned in select ctid from foo and then resultSet.getString(1);
deleteStmt.setObject(1, pgo);

int a = deleteStmt.executeUpdate();
System.out.println("delete returns " + a);
于 2013-09-18T16:28:30.147 回答
0

老话题,但我想补充一下

where ctid = ?::tid";

代替

where ctid = ?::ctid";

成功了。然后可以使用 PreparedStatement.setString(x, "(10,3)")

于 2018-06-01T14:21:14.650 回答