【问题标题】:What is the JDBC type for ctid in postgres?postgres 中 ctid 的 JDBC 类型是什么?
【发布时间】: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


【解决方案1】:

解决了!您必须手动创建 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);

【讨论】:

    【解决方案2】:

    老话题,但我想补充一下这段文字

    where ctid = ?::tid";
    

    而不是

    where ctid = ?::ctid";
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 2010-09-26
      • 1970-01-01
      • 2021-11-20
      • 2021-11-11
      相关资源
      最近更新 更多