BFILE编程示例¶
BFILE
/*创建环境句柄、到数据库的连接 和 statement*/
Environment *env = Environment::createEnvironment();
Connection *conn = env->createConnection(user,pwd,link);
Statement *stmt = conn->createStatement();
string csql = "create table testbfile(id INT ,bf BFILE)";
string dsql = "drop table testbfile";
string isql = "insert into occi_test values (:bl)";
string test_sql ="select count(*) from occi_test where bl is null";
ResultSet* rs = NULL;
//创建表
stmt->execute(dsql);
stmt->execute(csql);
// 创建目录
string createDir;
buffer = getcwd(NULL, 0);
createDir = "create or replace directory BFILEDIR as '" + buffer + "';";
m_stmt->executeQuery(createDir);
// 创建文件
ofstream fout("testbfile.txt",ios::out | ios::trunc);
fout << "唧唧复唧唧,木兰当户织。\n";
fout.close();
// 插入数据
stmt->setSQL(insert_sql);
stmt->setInt(1, 1016);
Bfile file(conn);
file.setName("BFILEDIR", "testbfile.txt");
stmt->setBfile(2, file);
stmt->execute();
conn->commit();
// 查询
while (rs->next())
{
char* buffer;
string dir, filename;
int len = 0;
int amt = 0;
int readlen = 0;
Bfile file = rs->getBfile(1);
if (!file.isOpen())
{
//打开文件
file.open();
}
len = file.length();
amt = len / 10;
buffer = new char[amt+1];
for (int i = 1; i <len; i+= readlen)
{
readlen = file.read(amt, (unsigned char*)buffer, amt+1, i);
cout << buffer << endl;
}
}
stmt->closeResultSet(rs);
m_stmt->executeQuery("drop table testbfile");
remove("testbfile.txt");
m_stmt->execute(dsql);
/*释放statement、数据库连接 和 环境句柄*/
conn->terminateStatement(stmt);
env->terminateConnection(conn);
Environment::terminateEnvironment(env);