Bulk Classes and RWDBBlob
Example 14 uses classes RWDBBulkInserter and RWDBBulkReader along with RWDBTBuffer<T> to insert and read binary data. To use this code, the <ver> placeholder in any library names would need to be replaced with the actual digits representing the library version number.
Example 14 – Bulk classes and RWDBBlob
#include <rw/rstream.h>
#include <rw/db/db.h>
#include <rw/db/tbuffer.h>
 
int main()
{
RWDBDatabase db = RWDBManager::database(
"ctl<ver>15d.dll", // Access Module type
"aServer", // server name
"aUser", // user name
"aPassword", // password
"aDB" // database name
);
 
// get a connection
RWDBConnection cn = db.connection();
 
// create an example table.
RWDBTable tbl = db.table("binExample");
if( tbl.exists(cn) ) tbl.drop(cn);
RWDBSchema sch;
sch.appendColumn("cBlob", RWDBValue::Blob, 30);
db.createTable(tbl.name(), sch, cn);
 
// scope this section so that the RWDBBulkInserter
// will release the connection after inserting.
// This is also needed for Sybase to finish bulk insertion.
 
{
// insert some data
RWDBTBuffer<RWDBBlob> binBuffer(5);
RWDBBlob aBlob("hello there\0again", 17);
for (size_t i = 0; i < 5; ++i)
binBuffer[i] = aBlob;
RWDBBulkInserter ins = tbl.bulkInserter(cn);
ins << binBuffer;
ins.execute();
}
 
// scoped so that RWDBBulkReader releases the connection
{
// now read back the data and print it to the screen
RWDBBulkReader rdr = tbl.bulkReader(cn);
RWDBTBuffer<RWDBBlob> binBuffer(5);
rdr << binBuffer;
RWDBBlob aBlob;
size_t rowsRead;
while (rowsRead = rdr()) {
for (size_t i = 0; i < rowsRead; ++i) {
aBlob = binBuffer[i];
std::cout << "the blob's length is: "
<< aBlob.length() << std::endl;
std::cout << "the blob's data is: ";
unsigned char* ptr = aBlob.data();
for (size_t j = 0; j < aBlob.length(); ++j, ++ptr) {
std::cout.put(*ptr);
}
std::cout << std::endl;
}
}
}
 
tbl.drop(cn);
return 0;
}
For more information on bulk reading and writing, see the entries in the SourcePro API Reference Guide on RWDBBulkInserter and RWDBBulkReader, and their related class RWDBTBuffer<T>.