尽管是抄录OTN,总有些帮助吧occiblob.cpp
The following code example demonstrates how to read and write aBLOB: #include <iostream.h>
#include <occi.h>
using namespace oracle::occi;
using namespace std;/**
* The demo sample has starts from startDemo method. This method is called
* by main. startDemo calls other methods, the supporting methods for
* startDemo are,
* insertRows - insert the rows into the table
* deleteRows - delete the rows inserted
* insertBlob - Inserts a blob and an empty_blob
* populateBlob - populates a given blob
* dumpBlob - prints the blob as an integer stream
*/class demoBlob
{
private:
string username;
string password;
string url; void insertRows (Connection *conn)
throw (SQLException)
{
Statement *stmt = conn->createStatement ("INSERT INTO
print_media(product_id,ad_id,ad_composite,ad_sourcetext) VALUES
(6666,11001,'10001','SHE')");
stmt->executeUpdate();
stmt->setSQL ("INSERT INTO
print_media(product_id,ad_id,ad_composite,ad_sourcetext) VALUES
(7777,11001,'1010','HEM')");
stmt->executeUpdate();
conn->commit();
conn->terminateStatement (stmt); } void deleteRows (Connection *conn)
throw (SQLException)
{
Statement *stmt = conn->createStatement ("DELETE print_media WHERE
product_id = 6666 AND ad_id=11001");
stmt->executeUpdate();
stmt->setSQL ("DELETE print_media WHERE product_id = 7777 AND ad_id=11001");
stmt->executeUpdate();
conn->commit();
conn->terminateStatement (stmt); } /**
* populating the blob;
*/
void populateBlob (Blob &blob, int size)
throw (SQLException)
{
Stream *outstream = blob.getStream (1,0);
char *buffer = new char[size]; memset (buffer, (char)10, size);
outstream->writeBuffer (buffer, size);
char *c = (char *)"";
outstream->writeLastBuffer (c,0);
delete (buffer);
blob.closeStream (outstream);
} /**
* printing the blob data as integer stream
*/
void dumpBlob (Blob &blob, int size)
throw (SQLException)
{
Stream *instream = blob.getStream (1,0);
char *buffer = new char[size];
memset (buffer, NULL, size); instream->readBuffer (buffer, size);
cout << "dumping blob: ";
for (int i = 0; i < size; ++i)
cout << (int) buffer[i];
cout << endl; delete (buffer);
blob.closeStream (instream);
} /**
* public methods
*/
public:
demoBlob ()
{
/**
* default values of username & password
*/
username = "SCOTT";
password = "TIGER";
url = "";
} void setUsername (string u)
{
username = u;
} void setPassword (string p)
{
password = p;
} void setUrl (string u)
{
url = u;
} void runSample ()
throw (SQLException)
{
Environment *env = Environment::createEnvironment (
Environment::DEFAULT);
try
{
Connection *conn = env->createConnection (username, password, url);
Statement *stmt1;
insertRows (conn);
/**
* Reading a populated blob & printing its property.
*/
string sqlQuery = "SELECT ad_composite FROM print_media WHERE
product_id=6666";
Statement *stmt = conn->createStatement (sqlQuery);
ResultSet *rset = stmt->executeQuery ();
while (rset->next ())
{
Blob blob = rset->getBlob (1);
cout << "Opening the blob in Read only mode" << endl;
blob.open (OCCI_LOB_READONLY);
int blobLength=blob.length ();
cout << "Length of the blob is: " << blobLength << endl;
dumpBlob (blob, blobLength);
blob.close ();
}
stmt->closeResultSet (rset);
/**
* Reading a populated blob & printing its property.
*/
stmt->setSQL ("SELECT ad_composite FROM print_media WHERE product_id =7777
FOR UPDATE");
rset = stmt->executeQuery ();
while (rset->next ())
{
Blob blob = rset->getBlob (1);
cout << "Opening the blob in read write mode" << endl;
blob.open (OCCI_LOB_READWRITE);
cout << "Populating the blob" << endl;
populateBlob (blob, 20);
int blobLength=blob.length ();
cout << "Length of the blob is: " << blobLength << endl;
dumpBlob (blob, blobLength);
blob.close ();
}
stmt->closeResultSet (rset);
deleteRows (conn);
conn->terminateStatement (stmt);
env->terminateConnection (conn);
}
catch (SQLException ea)
{
cout << ea.what();
}
Environment::terminateEnvironment (env);
}};//end of class demoBlobint main (void)
{
demoBlob *b = new demoBlob ();
b->setUsername ("SCOTT");
b->setPassword ("TIGER");
b->runSample ();
}
The following code example demonstrates how to read and write aBLOB: #include <iostream.h>
#include <occi.h>
using namespace oracle::occi;
using namespace std;/**
* The demo sample has starts from startDemo method. This method is called
* by main. startDemo calls other methods, the supporting methods for
* startDemo are,
* insertRows - insert the rows into the table
* deleteRows - delete the rows inserted
* insertBlob - Inserts a blob and an empty_blob
* populateBlob - populates a given blob
* dumpBlob - prints the blob as an integer stream
*/class demoBlob
{
private:
string username;
string password;
string url; void insertRows (Connection *conn)
throw (SQLException)
{
Statement *stmt = conn->createStatement ("INSERT INTO
print_media(product_id,ad_id,ad_composite,ad_sourcetext) VALUES
(6666,11001,'10001','SHE')");
stmt->executeUpdate();
stmt->setSQL ("INSERT INTO
print_media(product_id,ad_id,ad_composite,ad_sourcetext) VALUES
(7777,11001,'1010','HEM')");
stmt->executeUpdate();
conn->commit();
conn->terminateStatement (stmt); } void deleteRows (Connection *conn)
throw (SQLException)
{
Statement *stmt = conn->createStatement ("DELETE print_media WHERE
product_id = 6666 AND ad_id=11001");
stmt->executeUpdate();
stmt->setSQL ("DELETE print_media WHERE product_id = 7777 AND ad_id=11001");
stmt->executeUpdate();
conn->commit();
conn->terminateStatement (stmt); } /**
* populating the blob;
*/
void populateBlob (Blob &blob, int size)
throw (SQLException)
{
Stream *outstream = blob.getStream (1,0);
char *buffer = new char[size]; memset (buffer, (char)10, size);
outstream->writeBuffer (buffer, size);
char *c = (char *)"";
outstream->writeLastBuffer (c,0);
delete (buffer);
blob.closeStream (outstream);
} /**
* printing the blob data as integer stream
*/
void dumpBlob (Blob &blob, int size)
throw (SQLException)
{
Stream *instream = blob.getStream (1,0);
char *buffer = new char[size];
memset (buffer, NULL, size); instream->readBuffer (buffer, size);
cout << "dumping blob: ";
for (int i = 0; i < size; ++i)
cout << (int) buffer[i];
cout << endl; delete (buffer);
blob.closeStream (instream);
} /**
* public methods
*/
public:
demoBlob ()
{
/**
* default values of username & password
*/
username = "SCOTT";
password = "TIGER";
url = "";
} void setUsername (string u)
{
username = u;
} void setPassword (string p)
{
password = p;
} void setUrl (string u)
{
url = u;
} void runSample ()
throw (SQLException)
{
Environment *env = Environment::createEnvironment (
Environment::DEFAULT);
try
{
Connection *conn = env->createConnection (username, password, url);
Statement *stmt1;
insertRows (conn);
/**
* Reading a populated blob & printing its property.
*/
string sqlQuery = "SELECT ad_composite FROM print_media WHERE
product_id=6666";
Statement *stmt = conn->createStatement (sqlQuery);
ResultSet *rset = stmt->executeQuery ();
while (rset->next ())
{
Blob blob = rset->getBlob (1);
cout << "Opening the blob in Read only mode" << endl;
blob.open (OCCI_LOB_READONLY);
int blobLength=blob.length ();
cout << "Length of the blob is: " << blobLength << endl;
dumpBlob (blob, blobLength);
blob.close ();
}
stmt->closeResultSet (rset);
/**
* Reading a populated blob & printing its property.
*/
stmt->setSQL ("SELECT ad_composite FROM print_media WHERE product_id =7777
FOR UPDATE");
rset = stmt->executeQuery ();
while (rset->next ())
{
Blob blob = rset->getBlob (1);
cout << "Opening the blob in read write mode" << endl;
blob.open (OCCI_LOB_READWRITE);
cout << "Populating the blob" << endl;
populateBlob (blob, 20);
int blobLength=blob.length ();
cout << "Length of the blob is: " << blobLength << endl;
dumpBlob (blob, blobLength);
blob.close ();
}
stmt->closeResultSet (rset);
deleteRows (conn);
conn->terminateStatement (stmt);
env->terminateConnection (conn);
}
catch (SQLException ea)
{
cout << ea.what();
}
Environment::terminateEnvironment (env);
}};//end of class demoBlobint main (void)
{
demoBlob *b = new demoBlob ();
b->setUsername ("SCOTT");
b->setPassword ("TIGER");
b->runSample ();
}
解决方案 »
- DBCA创建完数据库之后进入SQLPlus创建用户缺提示无表空间
- linux下root用户连接oracle提示没有权限:insufficient privileges
- oracle store procedure語法問題
- 发个关于orical处女的帖子,大家来帮忙看看~~
- 怎样从oracle中读取blob数据
- 多个记录用order by排序后,如何用SQL语法只保留结果的第一条记录,而删除所有其它记录?
- oracle如何导出table的c语言结构?
- 登录oracle的OMS时为什么会出现用户登录身份证明不正确
- 这个sql怎么写
- 菜鸟提问:
- execute一个procedure,老是提示我权限不足,已经授了execute_catelog的权限?
- 奇怪,为什么这么慢!!
怎么都盯着OCCI呢?
那东西封装的不是很好
实施中肯定会出问题的
http://www.csis.gvsu.edu/GeneralInfo/Oracle/appdev.920/a96583.pdf