我怎样才能发现 PostgreSQL 中的 bytea 列包含任何数据?
How can I find out that bytea column in PostgreSQL contains any data?
我有包含 bytea 列的 IMAGE table。 bytea 列的值可以为空。我在这个 table:
中有三行
- id:1,name:"some string",data:null
- id: 2, name: "small.png", data: 包含小图(460 B)
- id: 3, name: "large.png", data: 包含大图(4.78 KB)
当我在 pgAdmin 中查看此 table 中的数据时,我看到:
从输出中我不知道 bytea 列中哪一行包含二进制数据,哪一行不包含二进制数据。
当我运行SQLselect:
select id, name, data from image;
我得到以下结果,从中我可以说 ID 为 2 的行包含一些二进制数据,但我无法区分其他行(ID 为 1 和 3 的行)是否有一些数据或为空:
问题
- 是否有任何SQL select选项可以查看bytea列中是否有任何数据?
- 是否有任何允许查看 bytea 列数据的 pgAdmin 设置?
为了清楚起见,我附上了 Java 测试代码,用于在 IMAGE table 中保存和检索数据。图片 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;
请注意,如果 data
为空,octet_length()
也将 return NULL
,因此您可能只需要它(对于零长度 bytea,它将 return 0
,所以你可以区分 null
值和空值)
由于我不使用 pgAdmin,所以我无法告诉您它是否具有查看二进制数据的特殊功能
我有包含 bytea 列的 IMAGE table。 bytea 列的值可以为空。我在这个 table:
中有三行- id:1,name:"some string",data:null
- id: 2, name: "small.png", data: 包含小图(460 B)
- id: 3, name: "large.png", data: 包含大图(4.78 KB)
当我在 pgAdmin 中查看此 table 中的数据时,我看到:
从输出中我不知道 bytea 列中哪一行包含二进制数据,哪一行不包含二进制数据。 当我运行SQLselect:
select id, name, data from image;
我得到以下结果,从中我可以说 ID 为 2 的行包含一些二进制数据,但我无法区分其他行(ID 为 1 和 3 的行)是否有一些数据或为空:
问题
- 是否有任何SQL select选项可以查看bytea列中是否有任何数据?
- 是否有任何允许查看 bytea 列数据的 pgAdmin 设置?
为了清楚起见,我附上了 Java 测试代码,用于在 IMAGE table 中保存和检索数据。图片 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;
请注意,如果 data
为空,octet_length()
也将 return NULL
,因此您可能只需要它(对于零长度 bytea,它将 return 0
,所以你可以区分 null
值和空值)
由于我不使用 pgAdmin,所以我无法告诉您它是否具有查看二进制数据的特殊功能