OCI中繫結變數的實現例子
/*CREATE TABLE TMP
(
HOSTNAME VARCHAR2(61 BYTE),
MODULENAME VARCHAR2(20 BYTE),
LOGTIME DATE NOT NULL,
LOGID NUMBER(3),
LOGLEVEL NUMBER(3),
LOGLEVELMASK NUMBER(3),
LOGCONTENT CLOB
)*/
(
HOSTNAME VARCHAR2(61 BYTE),
MODULENAME VARCHAR2(20 BYTE),
LOGTIME DATE NOT NULL,
LOGID NUMBER(3),
LOGLEVEL NUMBER(3),
LOGLEVELMASK NUMBER(3),
LOGCONTENT CLOB
)*/
===========================================================
OCIBind *bnd1p = NULL; /* the first bind handle */
OCIBind *bnd2p = NULL; /* the second bind handle */
OCIBind *bnd3p = NULL; /* the third bind handle */
OCIBind *bnd4p = NULL; /* the fouth bind handle */
OCIBind *bnd5p = NULL;
OCIBind *bnd6p = NULL;
OCIBind *bnd7p = NULL;
OCIBind *bnd2p = NULL; /* the second bind handle */
OCIBind *bnd3p = NULL; /* the third bind handle */
OCIBind *bnd4p = NULL; /* the fouth bind handle */
OCIBind *bnd5p = NULL;
OCIBind *bnd6p = NULL;
OCIBind *bnd7p = NULL;
bool DBAccess::do_bind_insert_log_to_db(
char *hostname,
char *modulename,
char *logtime,
int logid,
int loglevel,
int loglevelmask,
char *logcontent)
{
sword errr;
if ( !DBConnected() ) // if DB not open, open it
if(!OpenDbConnect(database)) return false;
dlog_debug("DBAccess::do_bind_insert_log_to_db, OCIStmtPrepare,stmthp=%d,errhp=%d",stmthp,errhp);
char *insertsql = "INSERT INTO LOCATION_LOG(HOSTNAME, MODULENAME, LOGTIME, LOGID, LOGLEVEL, LOGLEVELMASK, LOGCONTENT)\
VALUES (:Vhostname, :Vmodulename, to_date(:Vlogtime,'YYYY-MM-DD HH24:MI:SS'), :Vlogid, :Vloglevel, :Vloglevelmask, :Vlogcontent)";
if ((errr=OCIStmtPrepare(stmthp,errhp, reinterpret_cast(insertsql), (ub4)strlen((char *)insertsql),OCI_NTV_SYNTAX,OCI_DEFAULT))!=OCI_SUCCESS)
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIStmtPrepare INSERT INTO LOCATION_LOG \n");
return false;
}
//char *hostname,1
if ((errr= OCIBindByName(stmthp, &bnd1p, errhp, (text *) ":Vhostname",strlen(":Vhostname"), (ub1 *)hostname , strlen(hostname)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS)
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIBindByName HOSTNAME \n");
return false;
}
//char *modulename,2
if ((errr= OCIBindByName(stmthp, &bnd2p, errhp, (text *) ":Vmodulename",strlen(":Vmodulename"), (ub1 *)modulename , strlen(modulename)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS)
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIBindByName MODULENAME \n");
return false;
}
//char *logtime,3
if ((errr= OCIBindByName(stmthp, &bnd3p, errhp, (text *) ":Vlogtime",strlen(":Vlogtime"), (ub1 *)logtime , strlen(logtime)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS)
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIBindByName LOGTIME \n");
return false;
}
//int logid,4
if ((errr= OCIBindByName(stmthp, &bnd4p, errhp, (text *) ":Vlogid",-1, (ub1 *) &logid, (sword) sizeof(logid), SQLT_INT,(void *) 0, (ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0,OCI_DEFAULT))!=OCI_SUCCESS)
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIBindByName LOGID \n");
return false;
}
//int loglevel,5
if ((errr= OCIBindByName(stmthp, &bnd5p, errhp, (text *) ":Vloglevel",-1, (ub1 *) &loglevel, (sword) sizeof(loglevel), SQLT_INT,(void *) 0, (ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0,OCI_DEFAULT))!=OCI_SUCCESS)
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIBindByName LOGLEVEL \n");
return false;
}
//int loglevelmask,6
if ((errr= OCIBindByName(stmthp, &bnd6p, errhp, (text *) ":Vloglevelmask",-1, (ub1 *) &loglevelmask, (sword) sizeof(loglevelmask), SQLT_INT,(void *) 0, (ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0,OCI_DEFAULT))!=OCI_SUCCESS)
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIBindByName LOGLEVELMASK \n");
return false;
}
char *hostname,
char *modulename,
char *logtime,
int logid,
int loglevel,
int loglevelmask,
char *logcontent)
{
sword errr;
if ( !DBConnected() ) // if DB not open, open it
if(!OpenDbConnect(database)) return false;
dlog_debug("DBAccess::do_bind_insert_log_to_db, OCIStmtPrepare,stmthp=%d,errhp=%d",stmthp,errhp);
char *insertsql = "INSERT INTO LOCATION_LOG(HOSTNAME, MODULENAME, LOGTIME, LOGID, LOGLEVEL, LOGLEVELMASK, LOGCONTENT)\
VALUES (:Vhostname, :Vmodulename, to_date(:Vlogtime,'YYYY-MM-DD HH24:MI:SS'), :Vlogid, :Vloglevel, :Vloglevelmask, :Vlogcontent)";
if ((errr=OCIStmtPrepare(stmthp,errhp, reinterpret_cast
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIStmtPrepare INSERT INTO LOCATION_LOG \n");
return false;
}
//char *hostname,1
if ((errr= OCIBindByName(stmthp, &bnd1p, errhp, (text *) ":Vhostname",strlen(":Vhostname"), (ub1 *)hostname , strlen(hostname)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS)
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIBindByName HOSTNAME \n");
return false;
}
//char *modulename,2
if ((errr= OCIBindByName(stmthp, &bnd2p, errhp, (text *) ":Vmodulename",strlen(":Vmodulename"), (ub1 *)modulename , strlen(modulename)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS)
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIBindByName MODULENAME \n");
return false;
}
//char *logtime,3
if ((errr= OCIBindByName(stmthp, &bnd3p, errhp, (text *) ":Vlogtime",strlen(":Vlogtime"), (ub1 *)logtime , strlen(logtime)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS)
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIBindByName LOGTIME \n");
return false;
}
//int logid,4
if ((errr= OCIBindByName(stmthp, &bnd4p, errhp, (text *) ":Vlogid",-1, (ub1 *) &logid, (sword) sizeof(logid), SQLT_INT,(void *) 0, (ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0,OCI_DEFAULT))!=OCI_SUCCESS)
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIBindByName LOGID \n");
return false;
}
//int loglevel,5
if ((errr= OCIBindByName(stmthp, &bnd5p, errhp, (text *) ":Vloglevel",-1, (ub1 *) &loglevel, (sword) sizeof(loglevel), SQLT_INT,(void *) 0, (ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0,OCI_DEFAULT))!=OCI_SUCCESS)
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIBindByName LOGLEVEL \n");
return false;
}
//int loglevelmask,6
if ((errr= OCIBindByName(stmthp, &bnd6p, errhp, (text *) ":Vloglevelmask",-1, (ub1 *) &loglevelmask, (sword) sizeof(loglevelmask), SQLT_INT,(void *) 0, (ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0,OCI_DEFAULT))!=OCI_SUCCESS)
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIBindByName LOGLEVELMASK \n");
return false;
}
//char *logcontent,7
if ((errr= OCIBindByName(stmthp, &bnd7p, errhp, (text *) ":Vlogcontent",strlen(":Vlogcontent"), (ub1 *)logcontent , strlen(logcontent)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS)
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIBindByName LOGCONTENT \n");
return false;
}
dlog_debug("DBAccess::do_bind_insert_log_to_db, OCIStmtExecute,stmthp=%d,errhp=%d \n",stmthp,errhp);
if ((errr=OCIStmtExecute(svchp,stmthp,errhp,1,(ub4)0,NULL,NULL,(ub4) OCI_DEFAULT))!=OCI_SUCCESS) //execute
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIStmtExecute \n");
return false;
}
dlog_debug("DBAccess::do_bind_insert_log_to_db, OCIStmtExecute,done \n");
if ((errr=OCITransCommit(svchp, errhp, (ub4) 0)) != OCI_SUCCESS) //commit
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIStmtCommit \n");
return false;
}
return true;
}
if ((errr= OCIBindByName(stmthp, &bnd7p, errhp, (text *) ":Vlogcontent",strlen(":Vlogcontent"), (ub1 *)logcontent , strlen(logcontent)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS)
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIBindByName LOGCONTENT \n");
return false;
}
dlog_debug("DBAccess::do_bind_insert_log_to_db, OCIStmtExecute,stmthp=%d,errhp=%d \n",stmthp,errhp);
if ((errr=OCIStmtExecute(svchp,stmthp,errhp,1,(ub4)0,NULL,NULL,(ub4) OCI_DEFAULT))!=OCI_SUCCESS) //execute
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIStmtExecute \n");
return false;
}
dlog_debug("DBAccess::do_bind_insert_log_to_db, OCIStmtExecute,done \n");
if ((errr=OCITransCommit(svchp, errhp, (ub4) 0)) != OCI_SUCCESS) //commit
{
CheckError(errr);
dlog_error("DBAccess::do_bind_insert_log_to_db Error when OCIStmtCommit \n");
return false;
}
return true;
}
說明:
1. OCI中DateTime的處理相當麻煩, 可以使用char來替換DateTime使用. 在構造SQL的時候, 使用to_date進行轉換. to_date(:Vlogtime,'YYYY-MM-DD HH24:MI:SS')
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/81/viewspace-709486/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 繫結變數的一個例子變數
- oracle中使用繫結變數的好處的例子Oracle變數
- 關於DSS中的繫結變數變數
- 繫結變數變數
- 關於pl/sql中的繫結變數SQL變數
- 繫結變數之繫結變數窺探(Bind Peeking)變數
- Oracle 繫結變數Oracle變數
- 關於sql_profile中的繫結變數SQL變數
- ORACLE優化實戰(繫結變數)Oracle優化變數
- 繫結變數的測試變數
- 繫結變數窺測的演變變數
- 在oracle的plsql中為cursor使用繫結變數OracleSQL變數
- 檢視繫結變數變數
- 繫結變數窺測變數
- PLSQL使用繫結變數SQL變數
- Oracle之繫結變數Oracle變數
- 關於繫結變數變數
- 關於繫結變數的SQL繫結什麼值變數SQL
- 繫結變數的使用範圍變數
- oracle繫結變數的測試Oracle變數
- 查詢繫結變數的值變數
- 關於繫結變數的使用變數
- 【優化】使用繫結變數 OR 不使用繫結變數,這不是問題!優化變數
- ORACLE 繫結變數用法總結Oracle變數
- 使用繫結變數的一點總結!變數
- OLTP系統中儘量使用繫結變數變數
- 【最佳化】使用繫結變數 OR 不使用繫結變數,這不是問題!變數
- PL/SQL中繫結變數使用的簡單測試SQL變數
- 在Lua中實現Rust物件的繫結Rust物件
- Oracle 變數繫結與變數窺視合集Oracle變數
- Oracle 繫結變數窺探Oracle變數
- oracle 繫結變數(bind variable)Oracle變數
- 如何獲取繫結變數變數
- Oracle 繫結變數 詳解Oracle變數
- 檢視未繫結變數的sql變數SQL
- 獲取sql繫結變數的值SQL變數
- 從不繫結變數與繫結變數兩種情況討論柱狀圖的作用變數
- 如何實現VM框架中的資料繫結框架