如何在 Informix 上将二进制 blob 写入磁盘

发布于 2024-08-05 14:31:06 字数 67 浏览 4 评论 0原文

我在 informix 数据库中有一些图像,作为二进制 blob 字段 (jpg),我如何使用 SQL 将图像写入磁盘?

I have some images in an informix database, as a binary blob field (jpg), how can i write the images onto disk with an SQL?

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

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

发布评论

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

评论(2

暖树树初阳… 2024-08-12 14:31:06

数据存储在 BYTE 字段还是 BLOB 字段中?

如果数据存储在 BLOB 列中,那么您可以使用:

SELECT LOTOFILE(blob_column, '/path/to/file/on/client', 'client')
  FROM TheTable
 WHERE PK_Column = 23411   -- PK value

如果数据存储在 BYTE 列中,那么您必须更加努力地工作。如果您的计算机上有 ESQL/C (ClientSDK) 和 C 编译器,那么我建议从 IIUG Software 获取 SQLCMD存档并解压软件。您需要 Informix 环境集,并且需要能够编译 C 程序。然后运行:

./configure --prefix=$HOME/bin

您指定什么作为前缀并不重要 - 您只需要运行配置脚本即可。

然后,您可以编译所有内容 (make),也可以简单地编译程序 selblob (make selblob)。这个程序就是我所说的“小插曲”;一个微观程序,展示如何选择 BYTE blob 到磁盘。然而,它也是功能齐全的;它几乎可以处理您扔给它的任何东西,或者诊断错误。

如果您的数据库名为 precious,则字节数据位于表 byte_table 中,保存数据的列为 byte_column,主键列分别是 col1(所需值为 23)和 col2(所需值为 “Habeas Corpus” ),然后您可以运行:

selblob -d precious -t byte_table -k col1=23 -k col2="Habeas Corpus" \
        -c byte_column -f output_file

这会将字节值卸载到指定文件中。

如果您没有 ESQL/C 或 C 编译器或没有使用它们的权限,那么生活会更加困难。最接近的方法是使用 DB-Access 中的 UNLOAD 语句:

dbaccess precious - <<!
unload to "output_file"
select byte_column from byte_table where col1 = 23 and col2 = 'Habeas Corpus';
!

这将创建一个包含字节值(每个字符 2 个字节)的十六进制转储的文件。然后,您需要对文件进行后处理,将十六进制转换为常规数据。请注意,如果该列是 TEXT 列而不是 BYTE 列,则不需要转换。您可以使用相当简单的 Perl 脚本来进行转换(前提是文件足够小,可以放入内存中 - 如果文件不够小,您必须更加努力):

perl -w -e '
    $/ = "";
    my $data = <>;
    while (length($data) > 1)
    {
        my $hex = substr($data, 0, 2);
        printf "%c", hex($hex);
        $data = substr($data, 2);
    }' <output_file

长度条件指定 '>; 1' 处理卸载数据末尾的换行符。

(对于“歇斯底里的葡萄干”,又名“历史原因”,我仍然将 BYTE 和 TEXT 称为“blob 类型”,尽管 IDS 9.00 为“智能 blob”引入了显式名称 BLOB 和 CLOB,这是一对略有不同的数据类型大致对应的功能 - 在我的书中,它们都是 blob(小写)类型 这就是 1990 年(比 BLOB 和 CLOB blob 被添加六年或更长时间)了解 BYTE 和 TEXT blob 的老家伙们的麻烦。
无论如何,对于旧式斑点没有一个好的替代官方术语。使用“愚蠢的斑点”在政治上是不正确的!)

Is the data stored in a BYTE or a BLOB field?

If the data is stored in a BLOB column, then you can use:

SELECT LOTOFILE(blob_column, '/path/to/file/on/client', 'client')
  FROM TheTable
 WHERE PK_Column = 23411   -- PK value

If the data is stored in a BYTE column, then you have to work rather harder. If you have ESQL/C (ClientSDK) and a C compiler on your machine, then I recommend obtaining SQLCMD from the IIUG Software Archive and extracting the software. You need your Informix environment set, and you need to be able to compile C programs. Then run:

./configure --prefix=$HOME/bin

It doesn't much matter what you specify as the prefix - you just need to run the configure script.

You can then either compile everything (make), or you can simply compile the program selblob (make selblob). That program is what I call a 'vignette'; a microscopic program that shows how to select a BYTE blob to disk. It is, however, also fully-functional; it will work with just about anything that you throw at it, or diagnose an error.

If your database is called precious, the byte data is in a table byte_table, the column holding the data is byte_column, and the primary key columns are col1 (and the value required is 23) and col2 (and the value required is "Habeas Corpus"), then you can run:

selblob -d precious -t byte_table -k col1=23 -k col2="Habeas Corpus" \
        -c byte_column -f output_file

This will unload the byte value into the named file.

If you don't have ESQL/C or a C compiler or permission to use them, then life is more difficult. The closest approach is to use the UNLOAD statement in DB-Access:

dbaccess precious - <<!
unload to "output_file"
select byte_column from byte_table where col1 = 23 and col2 = 'Habeas Corpus';
!

This will create a file containing a hex-dump of the byte value (2 bytes per character). You then need to post-process the file to convert the hex into regular data. Note that if the column was a TEXT column instead of a BYTE column, then no conversion would be needed. You can use a fairly simple Perl script to do the conversion (provided the file is small enough to be slurped into memory - you have to work harder if it is not small enough):

perl -w -e '
    $/ = "";
    my $data = <>;
    while (length($data) > 1)
    {
        my $hex = substr($data, 0, 2);
        printf "%c", hex($hex);
        $data = substr($data, 2);
    }' <output_file

The length condition specifies '> 1' to deal with the newline at the end of the unloaded data.

(For 'hysterical raisins', aka 'historical reasons', I still call both BYTE and TEXT 'blob types', even though IDS 9.00 introduced the explicit names BLOB and CLOB for 'smart blobs', a slightly different pair of data types with roughly corresponding functionality - in my book, they're all blob (lower-case) types. That's the trouble with old guys who learned about BYTE and TEXT blobs in 1990, six years or more before BLOB and CLOB blobs were added.
In any case, there isn't a good alternative official terminology for the older style blobs; using 'dumb blobs' is not politically correct!)

薄荷梦 2024-08-12 14:31:06

您需要编写一个小程序来查询数据库并将 blob 保存到磁盘。大多数数据库没有“在磁盘上打开文件”的概念。

You need to write a small program that queries the database and saves the blobs to disk. Most databases have no notion of "open file on disk".

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