读取和写入LOB

有两种读写lob的通用方法:非流的和流的。

读取LOB

例5-1演示了如何使用非流方法从一个非空的内部LOB中获取数据。该方法要求跟踪读偏移量和剩余待读量,并将这些值传递给read()方法。

例5-2类似于它演示了如何通过使用非流方式从BFILE读取数据,其中BFILE定位器不是NULL。

与例5-1和例5-2相比,例5-3中演示的非空BLOB上的流读取不需要跟踪偏移量。

示例5-1 如何以非流模式读取blob

ResultSet *rset=stmt->executeQuery("SELECT ad_composite FROM print_media WHERE product_id=6666");

while(rset->next())

{

   Blob blob=rset->getBlob(1);

   if(blob.isNull())

     cerr <<"Null Blob"<<endl;

   else

   {

      blob.open(ACCI_LOB_READONLY);


      const unsigned int BUFSIZE=100;

      unsigned char buffer[BUFSIZE];

              meset(buffer,0,100);

      unsigned int readAmt=BUFSIZE;

      unsigned int offset=1;



      //从偏移量1读取readAmt字节

      blob.read(readAmt,buffer,BUFSIZE,offset);


      //缓冲器中的处理信息      ...

      blob.close();

   }

}

stmt->closeResultSet(rset);

5-2 如何以非流模式读取BFILE

ResultSet *rset=stmt->executeQuery("SELECT ad_graphic FROM print_media

                                    WHERE product_id=6666");

while(rset->next())

{

   Bfile file=rset->getBfile(1);

   if(bfile.isNull())

      cerr <<"Null Bfile"<<endl;

   else

   {

      //显示BFILE的目录别名和文件名

      cout <<"File Name:"<<bfile.getFileName()<<endl;

      cout <<"Directory Alias:"<<bfile.getDirAlias()<<endl;


     if(bfile.fileExists())

      {

         unsigned int length=bfile.length();

         char *buffer=new char[length];

         bfile.read(length, buffer, length, 1);

         //将BFILE的所有内容读入缓冲区,然后进行处理

         ...

         delete[] buffer;

      }

      else

         cerr <<"File does not exist"<<endl;

   }

}

stmt->closeResultSet(rset);

示例5-3 如何流式读取BLOB

暂时不支持getChunkSize();

写入LOB

例5-4演示了如何使用非流式写操作向内部非空LOB写入数据。

例5-5演示了如何使用流式写将数据写入内部LOB。

示例5-4 如何使用非流式写操作向内部非空LOB写入数据

ResultSet *rset=stmt->executeQuery("SELECT bb FROM acciblob WHERE id=18");

     while(rset->next())

{

Blob blob=rset->getBlob(1);

   if(blob.isNull())

    cerr <<"Null Blob"<<endl;

      else

    {




     blob.open(ACCI_LOB_READWRITE);

     unsigned int offset=1;

     offset+=b.length();    //计算初始偏移量

     const unsigned int BUFSIZE=10;

     unsigned char buffer[BUFSIZE];

     memset(buffer,0,BUFSIZE);

     unsigned int writeAmt=BUFSIZE;

     char *j;

    j="shentongshujuku";

    int i=0;

    while(i<strlen(j))

{

    if(strlen(j)-i<10){

        int k=strlen(j)-i;

        memcpy(buffer,j+i,k);

        blob.write(k, buffer, BUFSIZE, offset);

        i+=10;

}
    else{
        memcpy(buffer,j+i,10);

        blob.write(writeAmt, buffer, BUFSIZE, offset);

        offset += writeAmt;

        i+=10;
        }

}

blob.close();

    }

}

stmt->closeResultSet(rset);

conn->commit();

示例5-5 如何使用流式写将数据写入内部LOB

ResultSet *rset=stmt->executeQuery("SELECT bb FROM acciblob WHERE id=2  for update");


   while(rset->next())

{
   Blob blob=rset->getBlob(1);

   if(blob.isNull())

     cerr <<"Null Blob"<<endl;

    else

{
    blob.open(ACCI_LOB_READWRITE);

    const unsigned int BUFSIZE=10;

    char buffer[BUFSIZE];

    unsigned int offset=1;

    offset+= blob.length();

    Stream *outstream=blob.getStream(offset,0);

    char *j;

    j="shentongshujuku";

    int i=0;

    while(i<strlen(j))

  {

       if (strlen(j)-i<=10) {

         int k=strlen(j)-i;

         memcpy(buffer,j+i,k);

         outstream->writeLastBuffer(buffer,k);

         i+=10;

        }

        else {

        memcpy(buffer,j+i,10);

        outstream->writeBuffer(buffer,BUFSIZE);

        i+=10;

        }

}

blob.closeStream(outstream);

    }

}

stmt->closeResultSet(rset);

conn->commit();