如何使用oracle ODBC获取BLOB数据 [英] How to get BLOB data using oracle ODBC

查看:705
本文介绍了如何使用oracle ODBC获取BLOB数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取存储在BLOB中的图像,然后将其保存为jpg。

I'm trying to get image that are stored in BLOB and then save it as jpg.

这里我检索二进制数据并将其保存在str

Here i retrieve the binary data and save it in str;

  string str;
  SQLCHAR buf[500] = {0};
  while ((SQL_SUCCEEDED(SQLGetData(StmtHandle, colnum, SQL_C_BINARY, buf, sizeof(buf), NULL))))
  {
     string data(reinterpret_cast< const char* >(buf), reinterpret_cast< const char* >(buf) +    sizeof(buf));
     str = str + data;
  }

然后将其写入文件

ofstream file;  
file.open("C:\\Users\\tom\\Desktop\\img.jpeg");
file << str;
file.close();

我得到不正确的图像。

and i get the incorrect image.

这种数据提取方法有什么问题(我使用 this )?

What's wrong with this method of data extraction (i used this) ?

推荐答案

不熟悉ODBC编程,但一见钟情,我可以看到的一个问题是,你假设你的数据长度是你的缓冲区大小的倍数。但是最后一次读取并不能保证能够准确地返回 500字节的数据。

I'm not familiar with ODBC programming, but at first sight, one issue I can see is that you assume your data length is multiple of your buffer size. But the last read is not guaranteed to return exactly 500 bytes of data.

你应该这样写。也许:

  string str;
  SQLCHAR buf[500];
  SQLLEN cbLeft;      // #bytes remained

  while ((SQL_SUCCEEDED(SQLGetData(StmtHandle, 
                                   colnum, 
                                   SQL_C_BINARY, 
                                   buf, 
                                   sizeof(buf),
                                   &cbLeft))))
  //                               ^^^^^^^
  {
          string data(reinterpret_cast< const char* >(buf),
                      reinterpret_cast< const char* >(buf)
                      + cbLeft);
          //            ^^^^^^
          str = str + data;

请花几分钟时间查看使用长度/指标值,以检查长度/指标值

Please take a few minutes to review Using Length/Indicator Values in order to check how the length/indicator value is used.

这篇关于如何使用oracle ODBC获取BLOB数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆