我怎样才能发现PostgreSQL中的bytea列包含任何数据?

我怎样才能发现PostgreSQL中的bytea列包含任何数据?,postgresql,pgadmin,Postgresql,Pgadmin,我有一个带有bytea列的图像表。bytea列的值可以为空。我在这个表中有三行: id:1,名称:“某些字符串”,数据:null id:2,名称:“small.png”,数据:包含小图像(460B) id:3,名称:“large.png”,数据:包含较大的图像(4.78 KB) 在pgAdmin中查看此表中的数据时,我看到: 从输出中,我不知道哪一行包含bytea列中的二进制数据,哪一行不包含。 运行SQL select时: select id, name, data from image

我有一个带有bytea列的图像表。bytea列的值可以为空。我在这个表中有三行:

  • id:1,名称:“某些字符串”,数据:null
  • id:2,名称:“small.png”,数据:包含小图像(460B)
  • id:3,名称:“large.png”,数据:包含较大的图像(4.78 KB)
在pgAdmin中查看此表中的数据时,我看到:

从输出中,我不知道哪一行包含bytea列中的二进制数据,哪一行不包含。 运行SQL select时:

select id, name, data from image;
我得到以下结果,从中我可以说id为2的行包含一些二进制数据,但我无法区分其他行(id为1和3的行)是否有一些数据或为空:

问题

  • 是否有任何SQL select选项可用于查看bytea列中是否有任何数据
  • 是否有任何pgAdmin设置允许查看bytea列数据

为了澄清,我附上了Java测试代码,用于在图像表中保存和检索数据。Image small.png的大小为460B,large.png的大小为4.78KB

private static final String JDBC_POSTGRESQL = "jdbc:postgresql://localhost/testDB?user=username&password=passwd";
private static File[] files = {new File("small.png"), new File("large.png")};

public static void main(String[] args) {
    // stores just the string
    try (Connection con = DriverManager.getConnection(JDBC_POSTGRESQL)) {
        PreparedStatement ps = con.prepareStatement("insert into image (name) values (?)");
        ps.setString(1, "some string");
        ps.executeUpdate();
    } catch (SQLException e1) {
        e1.printStackTrace();
    }

    // store images
    for (File file : files) {
        try (Connection con = DriverManager.getConnection(JDBC_POSTGRESQL)) {
            byte[] bytes = Files.readAllBytes(file.toPath());
            PreparedStatement ps = con.prepareStatement("insert into image (name, data) values (?, ?)");
            FileInputStream fis = new FileInputStream(file);
            ps.setString(1, file.getName());
            ps.setBinaryStream(2, fis, bytes.length);
            ps.executeUpdate();
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }

    // read from image table and create files
    try (Connection con = DriverManager.getConnection(JDBC_POSTGRESQL)) {
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select name, data from image");
        while (rs.next()) {
            File outputFile = new File("output_" + rs.getString("name"));
            FileOutputStream fos = new FileOutputStream(outputFile);
            if (rs.getBytes("data") != null) {
                fos.write(rs.getBytes("data"));
            }
        }
    } catch (SQLException | IOException e) {
        e.printStackTrace();
    }
}

您可以使用
is null
运算符检查
null
值,并使用
octet_length()
获取
bytea
列的实际长度:

select id, 
       name, 
       data is null as data_is_null, 
       octet_length(data) as data_length 
from image;
请注意,
octet\u length()
如果
data
为空,也将返回
NULL
,因此您可能只需要它(对于零长度字节,它将返回
0
,以便您可以区分
NULL
值和空值)


由于我不使用pgAdmin,我无法告诉您它是否具有查看二进制数据的特殊功能

我可以知道您使用哪个GUI而不是
pgAdmin
?@mr.incognito:(它确实支持显示二进制数据:)