Oracle数据库:如何读取BLOB?

发布于 2024-11-01 11:04:04 字数 104 浏览 4 评论 0 原文

我正在使用 Oracle 数据库,并且我想读取 BLOB 的内容。我该怎么做?

当我执行一个简单的 select 语句时,它仅返回“(BLOB)”(不带引号)。如何阅读实际内容?

I'm working with an Oracle database, and I would like to read the contents of a BLOB. How do I do this?

When I do a simple select statement, it merely returns "(BLOB)" (without the quotes). How do I read the actual contents?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

影子是时光的心 2024-11-08 11:04:04

您可以使用 UTL_RAW.CAST_TO_RAW(UTL_RAW.CAST_TO_VARCHAR2()) 转储十六进制值。

SELECT b FROM foo;
-- (BLOB)

SELECT UTL_RAW.CAST_TO_RAW(UTL_RAW.CAST_TO_VARCHAR2(b))
FROM foo;
-- 1F8B080087CDC1520003F348CDC9C9D75128CF2FCA49D1E30200D7BBCDFC0E000000

这很方便,因为这与插入 BLOB 列的格式相同:

CREATE GLOBAL TEMPORARY TABLE foo (
    b BLOB);
INSERT INTO foo VALUES ('1f8b080087cdc1520003f348cdc9c9d75128cf2fca49d1e30200d7bbcdfc0e000000');

DESC foo;
-- Name Null Type 
-- ---- ---- ---- 
-- B        BLOB 

但是,在某个点(2000 字节?),相应的十六进制字符串超过了 Oracle 的最大字符串长度。如果您需要处理这种情况,则必须结合 如何使用 href="http://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_lob.htm#ARPLS66748" rel="nofollow noreferrer">DMBS_LOB.SUBSTR 的文档 更复杂的方法将允许您查看 BLOB 的子字符串。

You can dump the value in hex using UTL_RAW.CAST_TO_RAW(UTL_RAW.CAST_TO_VARCHAR2()).

SELECT b FROM foo;
-- (BLOB)

SELECT UTL_RAW.CAST_TO_RAW(UTL_RAW.CAST_TO_VARCHAR2(b))
FROM foo;
-- 1F8B080087CDC1520003F348CDC9C9D75128CF2FCA49D1E30200D7BBCDFC0E000000

This is handy because you this is the same format used for inserting into BLOB columns:

CREATE GLOBAL TEMPORARY TABLE foo (
    b BLOB);
INSERT INTO foo VALUES ('1f8b080087cdc1520003f348cdc9c9d75128cf2fca49d1e30200d7bbcdfc0e000000');

DESC foo;
-- Name Null Type 
-- ---- ---- ---- 
-- B        BLOB 

However, at a certain point (2000 bytes?) the corresponding hex string exceeds Oracle’s maximum string length. If you need to handle that case, you’ll have to combine How do I get textual contents from BLOB in Oracle SQL with the documentation for DMBS_LOB.SUBSTR for a more complicated approach that will allow you to see substrings of the BLOB.

血之狂魔 2024-11-08 11:04:04

SQL Developer 可以将 blob 显示为图像(至少它适用于 jpeg)。在数据视图中,双击 BLOB 字段以获取“铅笔”图标。单击铅笔将出现一个对话框,您可以在其中选择“查看为图像”复选框。

SQL Developer can show the blob as an image (at least it works for jpegs). In the Data view, double click on the BLOB field to get the "pencil" icon. Click on the pencil to get a dialog that will allow you to select a "View As Image" checkbox.

寒尘 2024-11-08 11:04:04

如果内容不是太大,您还可以使用

SELECT CAST ( <blobfield> AS RAW( <maxFieldLength> ) ) FROM <table>;

SELECT DUMP ( CAST ( <blobfield> AS RAW( <maxFieldLength> ) ) ) FROM <table>;

这将显示十六进制值。

If the content is not too large, you can also use

SELECT CAST ( <blobfield> AS RAW( <maxFieldLength> ) ) FROM <table>;

or

SELECT DUMP ( CAST ( <blobfield> AS RAW( <maxFieldLength> ) ) ) FROM <table>;

This will show you the HEX values.

且行且努力 2024-11-08 11:04:04

如果您使用 Oracle 本机数据提供程序而不是 Microsoft 驱动程序,那么您可以获取所有字段类型

Dim cn As New Oracle.DataAccess.Client.OracleConnection
Dim cm As New Oracle.DataAccess.Client.OracleCommand
Dim dr As Oracle.DataAccess.Client.OracleDataReader

连接字符串不需要提供程序值,因此您可以使用类似以下内容的内容:

"Data Source=myOracle;UserID=Me;Password=secret"

打开连接:

cn.ConnectionString = "Data Source=myOracle;UserID=Me;Password=secret"
cn.Open()

附加命令并设置 Sql 语句

cm.Connection = cn
cm.CommandText = strCommand

Set获取大小。我使用 4000 因为它和 varchar 一样大 启动

cm.InitialLONGFetchSize = 4000

读取器并循环遍历记录/列

dr = cm.ExecuteReader

Do while dr.read()
    strMyLongString = dr(i)
Loop

您可以更具体地读取,例如 dr.GetOracleString(i) dr.GetOracleClob(i) 等。如果您首先确定列中的数据类型。如果您正在读取 LONG 数据类型,那么简单的 dr(i)dr.GetOracleString(i) 就可以正常工作。关键是确保 InitialLONGFetchSize 对于数据类型来说足够大。另请注意,本机驱动程序不支持数据读取器的 CommandBehavior.SequentialAccess,但您不需要它,而且 LONG 字段甚至不必是 select 语句中的最后一个字段。

If you use the Oracle native data provider rather than the Microsoft driver then you can get at all field types

Dim cn As New Oracle.DataAccess.Client.OracleConnection
Dim cm As New Oracle.DataAccess.Client.OracleCommand
Dim dr As Oracle.DataAccess.Client.OracleDataReader

The connection string does not require a Provider value so you would use something like:

"Data Source=myOracle;UserID=Me;Password=secret"

Open the connection:

cn.ConnectionString = "Data Source=myOracle;UserID=Me;Password=secret"
cn.Open()

Attach the command and set the Sql statement

cm.Connection = cn
cm.CommandText = strCommand

Set the Fetch size. I use 4000 because it's as big as a varchar can be

cm.InitialLONGFetchSize = 4000

Start the reader and loop through the records/columns

dr = cm.ExecuteReader

Do while dr.read()
    strMyLongString = dr(i)
Loop

You can be more specific with the read, eg dr.GetOracleString(i) dr.GetOracleClob(i) etc. if you first identify the data type in the column. If you're reading a LONG datatype then the simple dr(i) or dr.GetOracleString(i) works fine. The key is to ensure that the InitialLONGFetchSize is big enough for the datatype. Note also that the native driver does not support CommandBehavior.SequentialAccess for the data reader but you don't need it and also, the LONG field does not even have to be the last field in the select statement.

转身以后 2024-11-08 11:04:04

你用什么客户端? .Net、Java、Ruby、SQLPLUS、SQL 开发人员?你在哪里写了这个简单的 select 语句?

还有为什么要读取blob的内容,blob包含二进制数据,这样数据是不可读的。如果您想存储文本而不是二进制内容,则应该使用 clob 而不是 blob。

我建议您下载 SQL DEVELOPER: http://www .oracle.com/technetwork/developer-tools/sql-developer/overview/index.html 。使用SQL DEVELOPER就可以看到内容。

What client do you use? .Net, Java, Ruby, SQLPLUS, SQL DEVELOPER? Where did you write that simple select statement?

And why do you want to read the content of the blob, a blob contains binary data so that data is unreadable. You should use a clob instead of a blob if you want to store text instead of binary content.

I suggest that you download SQL DEVELOPER: http://www.oracle.com/technetwork/developer-tools/sql-developer/overview/index.html . With SQL DEVELOPER you can see the content.

×眷恋的温暖 2024-11-08 11:04:04

如果您有兴趣从 BLOB 获取纯文本(正文部分),您可以使用 CTX_DOC 包

例如,CTX_DOC.FILTER 过程可以“生成文档的纯文本或 HTML 版本”。请注意,CTX_DOC.FILTER 需要 BLOB 列上的索引。如果您不希望这样做,可以改用 CTX_DOC.POLICY_FILTER 过程,该过程不需要索引。

If you're interested to get the plaintext (body part) from a BLOB, you could use the CTX_DOC package.

For example, the CTX_DOC.FILTER procedure can "generate either a plain text or a HTML version of a document". Be aware that CTX_DOC.FILTER requires an index on the BLOB column. If you don't want that, you could use the CTX_DOC.POLICY_FILTER procedure instead, which doesn't require an index.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文