C編寫的OCI連接Oracle數(shù)據(jù)庫_第1頁
C編寫的OCI連接Oracle數(shù)據(jù)庫_第2頁
C編寫的OCI連接Oracle數(shù)據(jù)庫_第3頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、linux下用C編寫的OCI連接Oracle數(shù)據(jù)庫程序代碼2009-11-28 15:57206人閱讀評論(0)收藏舉報在Oracle 建立數(shù)據(jù)庫create table employees(employee_id number(3), name varchar2(20), sex varchar2(5), birthday date);alter session set nls_date_format = 'yyyy-mm-dd'insert into employees values('101', '耳溫槍 ', 'na',

2、'1999-05-03');insert into employees values('102', 'xiaoli ', 'na', '1999-02-03');insert into employees values('103', 'xiaozhang ', '男 ', '1999-02-03');insert into employees values('104', '五千萬 ', '男 ', 

3、9;1999-02-03');insert into employees values('105', '問問 ', '女 ', '1999-02-03');insert into employees values('106', '阿散酸 ', '女 ', '1999-02-03');insert into employees values('107', '阿斯頓 ', '男 ', '1999-02-03&

4、#39;);insert into employees values('108', '撒旦 ', 'na', '2001-5-05');insert into employees values ('001','張三 ','三 ','2001-07-05');insert into employees values('111','張三 ','一 ','2003-02-08');OCI代碼:/*gcc -D_

5、LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -I$ORACLE_HOME/rdbms/public -I$ORACLE_HOME/rdbms/demo -L$ORACLE_HOME/lib -lclntsh -o oracle_test oracle_test.c*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <oci.h>static OCIEnv *p_env;static OCIError *p_err;static

6、 OCISvcCtx *p_svc;static OCIStmt *p_sql;static OCIDefine *p_dfn = (OCIDefine *) 0;static OCIBind *p_bnd = (OCIBind *) 0;int main() int p_bvi; char *p_sli; char *p_sli1; int rc; char errbuf100; int errcode; char mysql20; p_sli=(char *)malloc(20); memset(p_sli,0,20); p_sli1=(char *)malloc(20); memset(

7、p_sli1,0,20); /* Initialize OCI evironment*/ rc = OCIEnvCreate(OCIEnv *) &p_env,OCI_DEFAULT,(dvoid *)0, (dvoid * (*)(dvoid *, size_t) 0, (dvoid * (*)(dvoid *, dvoid *, size_t)0, (void (*)(dvoid *, dvoid *) 0, (size_t) 0, (dvoid *) 0); /* Initialize handles */ rc = OCIHandleAlloc( (dvoid *) p_env

8、, (dvoid *) &p_err, OCI_HTYPE_ERROR, (size_t) 0, (dvoid *) 0); rc = OCIHandleAlloc( (dvoid *) p_env, (dvoid *) &p_svc, OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid *) 0);/* Connect to database server */ rc = OCILogon(p_env, p_err, &p_svc, "scott", 5, "tiger", 5, "orcl&qu

9、ot;, 4); /rc = OCILogon(p_env, p_err, &p_svc, "hr/hrorcl", 10, NULL, -1, NULL, -1); if (rc != 0) OCIErrorGet(dvoid *)p_err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR); printf("Error - %.*s/n", 512, errbuf); exit(8); else printf("

10、Connect to orcl successful! 已連接!/n"); /* Allocate and prepare SQL statement */ rc = OCIHandleAlloc(dvoid *) p_env, (dvoid *) &p_sql, OCI_HTYPE_STMT, (size_t) 0, (dvoid *) 0);/* set my sql statement */ strcpy(mysql,"select name from employees where emploee_id=:x"); strcpy(mysql, &q

11、uot;select * from employees ");/ strcpy(mysql, "insert into employees values('109', 'x12er', 'na', '1888-06-08')"); /strcpy(mysql,"delete from employees where emploee_id='108'");/ strcpy(mysql,"desc employees;"); rc = OCIStmt

12、Prepare(p_sql, p_err, mysql, (ub4) strlen(mysql), (ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT);/* Bind the values for the bind variables */ / p_bvi = 101; /* Use employee_id=102 */ / rc = OCIBindByName(p_sql, &p_bnd, p_err, (text *) ":x", / -1, (dvoid *) &p_bvi, sizeof(int), SQLT_INT,

13、(dvoid *) 0, / (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT);/* Define the select list items */ rc = OCIDefineByPos(p_sql, &p_dfn, p_err, 1, (dvoid *) p_sli, (sword) 20, SQLT_STR, (dvoid *) 0, (ub2 *)0, (ub2 *)0, OCI_DEFAULT); rc = OCIDefineByPos(p_sql, &p_dfn, p_err, 2, (dvoid *) p

14、_sli1, (sword) 20, SQLT_STR, (dvoid *) 0, (ub2 *)0, (ub2 *)0, OCI_DEFAULT);/* Execute the SQL statment */ rc = OCIStmtExecute(p_svc, p_sql, p_err, (ub4) 1, (ub4) 0, (CONST OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT);while (rc != OCI_NO_DATA) /* Fetch the remaining data */ printf("%s

15、 %s/n",p_sli,p_sli1); rc = OCIStmtFetch(p_sql, p_err, 1, 0, 0); /* if(rc != OCI_SUCCESS && rc != OCI_SUCCESS_WITH_INFO) printf("insert error!/n"); OCIErrorGet(dvoid *)p_err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR); else printf("insert success!/n&quo

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論