C++ OCCI 读写blob字段样例源码
来源:互联网 发布:m123软件 编辑:程序博客网 时间:2024/06/09 16:16
#include <stdio.h>#include <iostream>#include <afx.h>#include "include/occi.h"#define LINUXOCCI //避免函数重定义错误using namespace std;using namespace oracle::occi;BYTE *imageBuffer = NULL;long g_bufSize = -1;int WriteLob(Connection* conn, Blob& lob, UCHAR* pData, long nDataSize){unsigned int nChunkSize, nBufSize, nWrite, nOffset;UCHAR*pi;intretval = -1;try{if( pData==NULL || nDataSize<1 ){//lob.trim(0);lob.setNull();return 1;}nWrite = nOffset = 0;lob.open(OCCI_LOB_READWRITE);#if 1while( nOffset < nDataSize ){nChunkSize = lob.getChunkSize();pi = pData + nOffset;nBufSize = (unsigned int)nDataSize - nOffset;if( nChunkSize < nBufSize )nWrite = nChunkSize;elsenWrite = nBufSize;nWrite = lob.writeChunk(nWrite, pi, nBufSize, nOffset+1);nOffset += nWrite;}#elseStream* pStream = lob.getStream();pStream->writeBuffer((char*)pData, (unsigned int)(ga_uint8)nDataSize);pStream->writeLastBuffer("", 0);lob.closeStream(pStream);#endiflob.close();retval = 1;}catch(SQLException& e){//DOSQLException(e, conn);cout<<"write blob异常"<<endl;return -1;}return retval;}int main(){Environment* env = Environment::createEnvironment(Environment::DEFAULT);string uesr = "admin";string password = "admin";string srvName = "192.168.7.201:1521/orcl";Connection* conn;Statement* stmt = NULL;conn = env->createConnection(uesr, password, srvName);//创建连接//添加数据懂啊数据库中CFile fpRead;CFileException e;fpRead.Open("1.jpg",CFile::modeRead | CFile::typeBinary,&e);g_bufSize = fpRead.GetLength();imageBuffer = new BYTE[g_bufSize];int ret;try{if((ret = fpRead.Read(imageBuffer,g_bufSize))>0){TRACE("ret = %d", ret);stmt = conn->createStatement("insert into image values(:1,:2)");int n = 1;stmt->setInt(1, n);Blob tmpBlob(conn);tmpBlob.setEmpty();stmt->setBlob(2, tmpBlob);stmt->executeUpdate();stmt->setSQL("select imagedata from image where id = :1 for update");stmt->setInt(1, n);ResultSet* rs = stmt->executeQuery();rs->next();tmpBlob = rs->getBlob(1);WriteLob(conn, tmpBlob, (UCHAR*)imageBuffer, g_bufSize);if(rs != NULL)stmt->closeResultSet(rs);if(stmt != NULL)conn->terminateStatement(stmt);conn->commit();cout<<"插入图片数据完成"<<endl;}}catch(SQLException ex){cout<<ex.getErrorCode()<<endl;cout<<ex.getMessage()<<endl;return 0;}/**********************添加结束**********************///读数据库中的图片出来//ResultSet* rs = NULL;//try{//int n = 1;////int blobLen;//stmt = conn->createStatement("select imagedata from image where id = :1 for update");//stmt->setInt(1, n);//rs = stmt->executeQuery();//rs->next();//Blob tmpBlob(conn);//tmpBlob = rs->getBlob(1);//tmpBlob.open(OCCI_LOB_READWRITE);//blobLen = tmpBlob.length();//imageBuffer = new BYTE[blobLen];//tmpBlob.read(blobLen,imageBuffer,blobLen,1);//tmpBlob.close();//CFile fpWrite;//CFileException e;//fpWrite.Open("2.jpg",CFile::modeWrite | CFile::modeCreate,&e);//fpWrite.Write(imageBuffer,blobLen);//fpWrite.Close();//if(rs!=NULL)//stmt->closeResultSet(rs);//if(stmt!=NULL)//conn->terminateStatement(stmt);//env->terminateConnection(conn);////释放空间//if(imageBuffer!=NULL)//delete []imageBuffer;////cout<<"读取图片完成"<<endl;//}//catch(SQLException ex){//cout<<ex.getErrorCode()<<endl;//cout<<ex.getMessage()<<endl;//return 0;//}Environment::terminateEnvironment(env);return 0;}
1 0
- C++ OCCI 读写blob字段样例源码
- Java读写Blob和Clob字段源码
- .net C#,access,MDB数据库BLOB二进制字段的读写
- c++builder对数据库中BLOB字段读写
- C++Builder向DBF读写BLOB字段内容
- Tstream用来读写blob字段
- AE中读写Blob字段
- SQLITE3 读写二进制字段blob
- java读写Oracle Blob字段
- oracle中读写blob字段的问题
- JAVA读写Oracle中blob字段
- Hibernate读写Clob和Blob类型字段
- BCB实现BLOB字段的读写
- Oracle blob字段类型 文件读写实例
- Hibernate读写Clob和Blob类型字段
- 关于 Oracle Clob Blob 字段的读写
- Hibernate读写Clob和Blob类型字段
- C语言读写 mysql blob 二进制文件
- 堆的优先队列1
- 遇见C++ Lambda
- vlc命令行参数详解
- 【程序员编程艺术】第六章:求解500万以内的亲和数
- python操作Excel读写--使用xlrd
- C++ OCCI 读写blob字段样例源码
- dataGridView1属性之RowHeadersWidthSizeMode导致加载数据缓慢
- 引用library project在打包时conversion to dalvik format failed with error 1 library project
- 数据库架构的演变
- URL重写
- Android 开发之底层驱动开发(一)
- 【读书笔记】—时间管理小强升职记
- strcpy(str1,str2);
- 机房收费系统重构(二)—菜鸟入门