目录
目录
说明
本博客每天都有干货更新,欢迎关注收藏,转载请注明出名。更多关于Linux的技术学习与交流可加QQ群:927421758。
示例
以下展示各种使用示例。
基本使用方法示例
-
编码
//1.c
#include "ocilib.h"
int main(int argc, char *argv[])
{
OCI_Connection* cn;
OCI_Statement* st;
OCI_Resultset* rs;
OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT);
cn = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
st = OCI_StatementCreate(cn);
OCI_ExecuteStmt(st, "select intcol, strcol from table");
rs = OCI_GetResultset(st);
while (OCI_FetchNext(rs))
{
printf("%i - %s\n", OCI_GetInt(rs, 1), OCI_GetString(rs,2));
}
OCI_Cleanup();
return EXIT_SUCCESS;
}
-
编译
$gcc 1.c
/tmp/ccc94uSS.o: In function `main':
1.c:(.text+0x20): undefined reference to `OCI_Initialize'
1.c:(.text+0x39): undefined reference to `OCI_ConnectionCreate'
1.c:(.text+0x49): undefined reference to `OCI_StatementCreate'
1.c:(.text+0x5e): undefined reference to `OCI_ExecuteStmt'
1.c:(.text+0x6a): undefined reference to `OCI_GetResultset'
1.c:(.text+0x81): undefined reference to `OCI_GetString'
1.c:(.text+0x95): undefined reference to `OCI_GetInt'
1.c:(.text+0xba): undefined reference to `OCI_FetchNext'
1.c:(.text+0xc3): undefined reference to `OCI_Cleanup'
添加相关头路径和库路径及库链接再编译
$gcc 1.c -I/usr/loca/include -I/oracle/app/oracle/product/11.1.0/db_1/rdbms/public -L/oracle/app/oracle/product/11.1.0/db_1/lib -L/usr/local/lib –locilib
$ls
1.c a.out
生成编译脚本build.sh
cat build.sh
gcc 1.c -I/usr/loca/include -I/oracle/app/oracle/product/11.1.0/db_1/rdbms/public -L/oracle/app/oracle/product/11.1.0/db_1/lib -L/usr/local/lib -locilib
-
修改
#include "ocilib.h"
int main(int argc, char *argv[])
{
OCI_Connection* cn;
OCI_Statement* st;
OCI_Resultset* rs;
OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT);
cn = OCI_ConnectionCreate("192.168.1.220", "qgtg", "qgtg", OCI_SESSION_DEFAULT);
//cn = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
st = OCI_StatementCreate(cn);
OCI_ExecuteStmt(st, "select name, format, id from example");
rs = OCI_GetResultset(st);
while (OCI_FetchNext(rs))
{
printf("name:%s - format:%s - id:%d\n", OCI_GetString(rs,1), OCI_GetString(rs,2), OCI_GetInt(rs, 3));
}
OCI_Cleanup();
return EXIT_SUCCESS;
}
-
执行
./a.out
name:usrr1 - format:fff11 - id:123
name: us123 - format:f1 - id:456
连接与错误处理
#include "ocilibDemo.h"
void err_handler(OCI_Error *err)
{
printf(
"code : ORA-%05i\n"
"msg : %s\n"
"sql : %s\n",
OCI_ErrorGetOCICode(err),
OCI_ErrorGetString(err),
OCI_GetSql(OCI_ErrorGetStatement(err))
);
}
void conn()
{
OCI_Connection *cn;
if (!OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT))
return;
cn = OCI_ConnectionCreate(tnsName, user, password, OCI_SESSION_DEFAULT);
if (cn != NULL) {
printf(OCI_GetVersionServer(cn));
printf("Server major version : %i\n", OCI_GetServerMajorVersion(cn));
printf("Server minor version : %i\n", OCI_GetServerMinorVersion(cn));
printf("Server revision version : %i\n", OCI_GetServerRevisionVersion(cn));
printf("Connection version : %i\n", OCI_GetVersionConnection(cn));
/* ... application code here ... */
OCI_ConnectionFree(cn);
}
OCI_Cleanup();
return;
}
建表
void createTable(){
OCI_Statement *st = NULL;
OCI_Connection *cn;
if (!OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT))
return;
cn = OCI_ConnectionCreate(tnsName, user, password, OCI_SESSION_DEFAULT);
if (cn!=NULL) {
st = OCI_StatementCreate(cn);
char sql[]=MT("create table test_table ")
MT("( ")
MT("val_int number, ")
MT("val_flt float, ")
MT("val_str varchar2(30), ")
MT("val_date date")
MT(")");
cout<<"Create table:\n"<<sql<<endl;
OCI_ExecuteStmt(st, sql);
OCI_ConnectionFree(cn);
}
OCI_Cleanup();
}
删除表
void dropTable(){
OCI_Statement *st = NULL;
OCI_Connection *cn;
if (!OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT))
return;
cn = OCI_ConnectionCreate(tnsName, user, password, OCI_SESSION_DEFAULT);
if (cn!=NULL) {
st = OCI_StatementCreate(cn);
char sql[]=MT("drop table test_table");
cout<<"Drop table:\n"<<sql<<endl;
OCI_ExecuteStmt(st, sql);
OCI_ConnectionFree(cn);
}
OCI_Cleanup();
}
插入(绑定)
void insertBind(){
OCI_Date *date;
int i;
double flt;
OCI_Statement *st = NULL;
OCI_Connection *cn;
if (!OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT))
return;
cn = OCI_ConnectionCreate(tnsName, user, password, OCI_SESSION_DEFAULT);
if (cn!=NULL) {
st = OCI_StatementCreate(cn);
char sql[]=MT("insert into test_table ")
MT("( ")
MT(" val_int, val_flt, val_str, val_date")
MT( ") " )
MT( "values ")
MT( "( ")
MT( " :val_int, :val_flt, :val_str, :val_date")
MT(") ");
cout<<"Intsert table:\n"<<sql<<endl;
OCI_Prepare(st, sql);
i = 1;
flt = 3.14;
string s="sfsdfsdfsfsdfsdfsd";
date = OCI_DateCreate(cn);
OCI_DateSysDate(date);
OCI_BindInt(st, MT(":val_int"), &i);
OCI_BindDouble(st, MT(":val_flt"), &flt);
OCI_BindString(st, MT(":val_str"), const_cast<char *>(s.c_str()), 30);
OCI_BindDate(st, MT(":val_date"), date);
OCI_Execute(st);
OCI_DateFree(date);
OCI_Commit(cn);
OCI_ConnectionFree(cn);
}
OCI_Cleanup();
}
插入(非绑定)
void insertArray(){
OCI_Connection *cn;
OCI_Statement *st;
OCI_Error *err;
int count=20000000;
int batchSize=5000;
int tab_int[batchSize];
double tab_flt[batchSize];
char tab_str[batchSize][31];
OCI_Date* tab_date[batchSize];
int i;
if (!OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT))
return;
cn = OCI_ConnectionCreate(tnsName, user, password, OCI_SESSION_DEFAULT);
st = OCI_StatementCreate(cn);
OCI_Prepare(st, "insert into test_table values(:i, :f, :s, :t)");
OCI_BindArraySetSize(st, batchSize);
OCI_BindArrayOfInts(st, ":i", tab_int, 0);
OCI_BindArrayOfDoubles(st, ":f", tab_flt, 0);
OCI_BindArrayOfStrings(st, ":s", (char*) tab_str, 30, 0);
OCI_BindArrayOfDates(st,":t",tab_date,0);
for (i=0;i<batchSize;i++) {
tab_int[i] = i+1;
tab_flt[i]=3.14;
sprintf(tab_str[i],"Name %d",i+1);
tab_date[i] = OCI_DateCreate(cn);
OCI_DateSysDate(tab_date[i]);
}
int round=count/batchSize;
clock_t start=clock();
cout<<start<<endl;
for (int j=0;j<round;j++) {
if (!OCI_Execute(st)) {
printf("Number of DML array errors : %d\n", OCI_GetBatchErrorCount(st));
err = OCI_GetBatchError(st);
while (err) {
printf("Error at row %d : %s\n", OCI_ErrorGetRow(err), OCI_ErrorGetString(err));
err = OCI_GetBatchError(st);
}
}
OCI_Commit(cn);
// printf("row processed : %d\n", OCI_GetAffectedRows(st));
}
clock_t stop=clock();
cout<<stop<<endl;
int costTime=stop-start;
double numberPerSec=(count/costTime)*1000;
cout<<"Insert records "<<count<<" cost time "<<costTime<<" ms"<<endl;
cout<<"Insert records "<<numberPerSec<<"records/s "<<endl;
for (i=0;i<batchSize;i++) {
OCI_DateFree(tab_date[i]);
}
OCI_Commit(cn);
OCI_ConnectionFree(cn);
OCI_Cleanup();
return;
}
动态获取BLOB
$cd /usr/local/include
$grep "OCI_CLOB" * -R
/* lob types */
#define OCI_BLOB 1
#define OCI_CLOB 2
#define OCI_NCLOB 3
/* lob opening mode */
#define OCI_LOB_READONLY 1
#define OCI_LOB_READWRITE 2
/* file types */
#define OCI_BFILE 1