如何从具有blob值的oracle表生成postgresql的插入脚本

时间:2016-10-13 08:36:38

标签: postgresql

我有一个具有BLOB列的Oracle表。我需要生成一个与Postgres兼容的插入脚本。

我在Java中尝试了以下逻辑,但它不起作用:

String value = null;
switch ( dataType ) {
     case Types.BLOB:
        Blob blob = rs.getBlob(colName);
        if ( ( blob != null ) && ( !rs.wasNull() ) ) {
        int blobLength = (int) blob.length(); 
        byte[] blobAsBytes = blob.getBytes(1, blobLength);
        value = blobAsBytes.toString();                 
        break;
    }
return value;

1 个答案:

答案 0 :(得分:0)

您需要将byte[]数组转换为bytea列的the supported formats之一,例如E'\\xDEADBEEF'::bytea

类似的东西:

StringBuilder result = new StringBuilder(blobAsBytes.length * 2 + 12);
result.append("E'\\\\x");
for (int i = 0; i < blobAsBytes.length; i++)
{
  int c = (blobAsBytes[i] < 0 ? 256 + blobAsBytes[i] : blobAsBytes[i]);
  String hexString = Integer.toHexString(value);
  if (c < 16) result.append('0');
  result.append(hexString);
}
result.append("'::bytea');
value = result.toString();

我不完全确定是否需要::bytea演员,但它不会受到伤害。