下載下傳位址:http://vrogier.github.io/ocilib/download/
文檔目錄如下,将include和lib(選擇lib32還是lib64看自己需求)分别拷貝到自己項目路徑下
然後在配置,如下:
代碼寫好後,要将lib目錄下的dll檔案放在執行程式同路徑下
下面是api中的例子:
查詢資料
#include "ocilib.h"
int main(void)
{
OCI_Connection *cn;
OCI_Statement *st;
OCI_Resultset *rs;
if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT))
return EXIT_FAILURE;
cn = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
st = OCI_StatementCreate(cn);
OCI_ExecuteStmt(st, "select * from products");
rs = OCI_GetResultset(st);
while (OCI_FetchNext(rs))
printf("code: %i, name %s\n", OCI_GetInt(rs, 1) , OCI_GetString(rs, 2));
printf("\n%d row(s) fetched\n", OCI_GetRowCount(rs));
OCI_Cleanup();
return EXIT_SUCCESS;
}
條件查詢:
#include "ocilib.h"
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
OCI_Connection* cn;
OCI_Statement* st;
OCI_Resultset* rs;
OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT);
string db = "TC8";
cn = OCI_ConnectionCreate((otext*)db.c_str(), "infodba", "infodba", OCI_SESSION_DEFAULT);
string id = "ASHOK";
st = OCI_StatementCreate(cn);
OCI_Prepare(st,"select * from signoff_info WHERE ITEMID=:ID");
OCI_BindString(st,":ID",(otext*)id.c_str(),id.size());
OCI_Execute(st);
rs = OCI_GetResultset(st);
while (OCI_FetchNext(rs))
{
printf(" %s\n",OCI_GetString(rs,1));
}
OCI_Cleanup();
retrun EXIT_SUCCESS;
}
BLOB/CLOB/NCLOB:
#include "ocilib.h"
#define SIZE_BUF 512
int main(void)
{
OCI_Connection *cn;
OCI_Statement *st;
OCI_Resultset *rs;
OCI_Lob *lob1, *lob2;
char temp[SIZE_BUF+1];
int code, n;
if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT))
return EXIT_FAILURE;
cn = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
st = OCI_StatementCreate(cn);
OCI_ExecuteStmt(st, "select code, content from test_lob for update");
rs = OCI_GetResultset(st);
while (OCI_FetchNext(rs))
{
code = OCI_GetInt(rs, 1);
lob1 = OCI_GetLob(rs, 2);
lob2 = OCI_LobCreate(cn, OCI_CLOB);
n = OCI_LobWrite(lob1, "Today, ", 7);
OCI_LobSeek(lob1, n, OCI_SEEK_SET);
n = OCI_LobWrite(lob2, "I'm going to the cinema !", 25);
OCI_LobAppendLob(lob1, lob2);
OCI_LobSeek(lob1, 0, OCI_SEEK_SET);
n = OCI_LobRead(lob1, temp, SIZE_BUF);
temp[n] = 0;
printf("code: %i, action : %s\n", code, temp);
OCI_LobFree(lob2);
}
printf("\n%d row(s) fetched\n", OCI_GetRowCount(rs));
OCI_Cleanup();
return EXIT_SUCCESS;
}
BFILE/CFILE:
#include "ocilib.h"
int main(void)
{
OCI_Connection *cn;
OCI_Statement *st;
OCI_Resultset *rs;
OCI_File *file;
char buffer[256];
int n;
if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT))
return EXIT_FAILURE;
cn = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
st = OCI_StatementCreate(cn);
file = OCI_FileCreate(cn, OCI_CFILE);
OCI_FileSetName(file, "檔案路徑", "MyfileName");
/* check if faile exists */
if (OCI_FileExists(file))
{
printf("file size : %d\n", OCI_FileGetSize(file));
printf("file dir : %s\n", OCI_FileGetDirectory(file));
printf("file name : %s\n", OCI_FileGetName(file));
}
/* bind for inserting into table */
OCI_Prepare(st, "insert into my_bfile_table(code, value) values (1, :bfile)");
OCI_BindFile(st, ":bfile", file);
OCI_Execute(st);
OCI_Commit(cn);
/* free local file object */
OCI_FileFree(file),
/* fetch bfile data from table */
OCI_ExecuteStmt(st, "select code, value from my_bfile_table");
rs = OCI_GetResultset(st);
while (OCI_FetchNext(rs))
{
file = OCI_GetFile(rs, 2);
OCI_FileOpen(file);
printf("file size %d\n", OCI_FileGetSize(file));
printf("file dir %s\n", OCI_FileGetDirectory(file));
printf("file name %s\n", OCI_FileGetName(file));
while (n = OCI_FileRead(file, buffer, sizeof(buffer)-1))
{
buffer[n] = 0;
printf(buffer);
}
OCI_FileClose(file);
}
OCI_Cleanup();
return EXIT_SUCCESS;
}
時間:
#include "ocilib.h"
#define SIZE_STR 260
int main(void)
{
OCI_Date *d1, *d2;
char str[SIZE_STR+1];
if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT))
return EXIT_FAILURE;
d1 = OCI_DateCreate(NULL);
d2 = OCI_DateCreate(NULL);
strcpy(str, "13041978 20:20:12");
OCI_DateFromText(d1, str, "DDMMYYYY HH24:MI:SS");
OCI_DateToText(d1, "DD/MM/YYYY HH24:MI:SS", SIZE_STR, str);
printf("\nDate is %s\n", str);
OCI_DateSysDate(d1);
OCI_DateToText(d1, "DD/MM/YYYY HH24:MI:SS", SIZE_STR, str);
printf("\nSysdate is %s\n", str);
OCI_DateAddDays(d1, 5);
OCI_DateAddMonths(d1, 2);
OCI_DateToText(d1, "DD/MM/YYYY HH24:MI:SS", SIZE_STR, str);
printf("\nDate + 5 days and 2 months is %s\n", str);
OCI_DateAssign(d2, d1);
OCI_DateLastDay(d1);
OCI_DateToText(d1, "DD/MM/YYYY HH24:MI:SS", SIZE_STR, str);
printf("\nLast day of the month : %s\n", str);
printf("\nNumber of days until the end of the months : %i\n",
OCI_DateDaysBetween(d1, d2));
OCI_DateFree(d1);
OCI_DateFree(d2);
OCI_Cleanup();
return EXIT_SUCCESS;
}