在C語言中插入Oracle數據是一項必要的技能,不僅可以給我們帶來工作或學習上的便捷,而且更是提高自身編程能力的一種方式。那么,本文主要來講一下如何在C語言中插入Oracle數據,幫助大家更好地進行編程或工作。
我們假設我們有一個學生信息表,包含學號、姓名、性別、年齡、所在班級等基本信息。現在,我們需要將一位新的學生的信息插入到這個表中。那么,以下是一個簡單的示例,演示如何完成這個任務。
#include#include #include #include int main() { OCIEnv *envhp; OCIError *errhp; OCIServer *srvhp; OCISvcCtx *svchp; OCISession *authp; sword status; OCIStmt *stmthp; text *stmt = (text *)"INSERT INTO student (student_no, student_name, student_gender, student_age, class_name) VALUES (:1, :2, :3, :4, :5)"; ub4 p_student_no; char *p_student_name; char p_student_gender; int p_student_age; char *p_class_name; const ub4 MAX_FIELD_WIDTH = 512; p_student_no = 1001; p_student_name = "張三"; p_student_gender = '男'; p_student_age = 20; p_class_name = "計算機科學與技術1班"; text student_no[MAX_FIELD_WIDTH]; text student_name[MAX_FIELD_WIDTH]; text student_gender[MAX_FIELD_WIDTH]; text student_age[MAX_FIELD_WIDTH]; text class_name[MAX_FIELD_WIDTH]; OCIInitialize(OCI_THREADED, (dvoid *)0, (dvoid *(*)(size_t))malloc, (dvoid* (*)(void *))realloc,(void (*)(void *))free ); OCIEnvInit((OCIEnv **)&envhp, OCI_DEFAULT, (size_t)0,(dvoid **)0); OCIHandleAlloc((dvoid *)envhp, (dvoid **)&errhp, OCI_HTYPE_ERROR,(size_t)0, (dvoid **)0); OCIHandleAlloc((dvoid *)envhp, (dvoid **)&srvhp, OCI_HTYPE_SERVER,(size_t)0, (dvoid **)0); OCIServerAttach(srvhp, errhp, (text *)"mydb", strlen((char*)"mydb"), OCI_DEFAULT); OCIHandleAlloc((dvoid *)envhp, (dvoid **)&svchp, OCI_HTYPE_SVCCTX, (size_t)0, (dvoid **)0); status=OCIAttrSet((dvoid *)svchp, (ub4)OCI_HTYPE_SVCCTX, (dvoid *)srvhp,(ub4)0, OCI_ATTR_SERVER, (OCIError *)errhp); OCISessionBegin(svchp, errhp, &authp, OCI_CRED_RDBMS, OCI_DEFAULT); OCIHandleAlloc((dvoid *)envhp, (dvoid **)&stmthp, OCI_HTYPE_STMT, (size_t)0, (dvoid **)0); status = OCIStmtPrepare(stmthp, errhp, (text *)stmt, (ub4)strlen((char *)stmt), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT); status=OCIStmtBindByPos(stmthp, &p_student_no, sizeof(&p_student_no), (dvoid *)&student_no[0], MAX_FIELD_WIDTH, SQLT_INT, (dvoid *)0, (ub2 *)0, (ub2 *)0, (ub4)0, (ub4 *)0, OCI_DEFAULT); status=OCIStmtBindByPos(stmthp, &p_student_name, strlen(p_student_name), (dvoid *)&student_name[0], MAX_FIELD_WIDTH, SQLT_STR, (dvoid *)0, (ub2 *)0, (ub2 *)0, (ub4)0, (ub4 *)0, OCI_DEFAULT); status=OCIStmtBindByPos(stmthp, &p_student_gender, sizeof(&p_student_gender), (dvoid *)&student_gender[0], MAX_FIELD_WIDTH, SQLT_CHR, (dvoid *)0, (ub2 *)0, (ub2 *)0, (ub4)0, (ub4 *)0, OCI_DEFAULT); status=OCIStmtBindByPos(stmthp, &p_student_age, sizeof(&p_student_age), (dvoid *)&student_age[0], MAX_FIELD_WIDTH, SQLT_INT, (dvoid *)0, (ub2 *)0, (ub2 *)0, (ub4)0, (ub4 *)0, OCI_DEFAULT); status=OCIStmtBindByPos(stmthp, &p_class_name, strlen(p_class_name), (dvoid *)&class_name[0], MAX_FIELD_WIDTH, SQLT_STR, (dvoid *)0, (ub2 *)0, (ub2 *)0, (ub4)0, (ub4 *)0, OCI_DEFAULT); OCIStmtExecute(svchp, stmthp, errhp, (ub4)1, (ub4)0, (CONST OCISnapshot *)NULL, (OCISnapshot *)NULL, OCI_DEFAULT); OCISessionEnd(svchp, errhp, authp, OCI_DEFAULT); OCIHandleFree((dvoid *)stmthp, OCI_HTYPE_STMT); OCIHandleFree((dvoid *)svchp, OCI_HTYPE_SVCCTX); OCIServerDetach(srvhp, errhp, OCI_DEFAULT); OCIHandleFree((dvoid *)errhp, OCI_HTYPE_ERROR); OCIHandleFree((dvoid *)srvhp, OCI_HTYPE_SERVER); OCIHandleFree((dvoid *)envhp, OCI_HTYPE_ENV); return 0; }
上述代碼首先使用OCIEnvInit函數初始化了OCIEnv,接下來通過OCIAttrSet函數設置svchp句柄,使其與OCIServer關聯。之后,通過OCIStmtPrepare函數準備并編譯了SQL語句。接下來,使用OCIStmtBindByPos函數通過位置綁定變量,并在該函數中使用了text類型表示變量的類型,使用SQLT_INT、SQLT_STR和SQLT_CHR等常量表示變量的類型。最后通過OCIStmtExecute函數執行SQL語句即可完成數據插入。
當然,以上只是一個簡單的示例,對實際開發、具體的表結構、數據類型等都有很大的影響。這里僅提供一個思路,供大家參考。
總的來說,插入Oracle數據需要使用C語言結合OCI接口編寫相關代碼,雖然過程較為繁瑣,但仍能提高我們的編程能力和解決工作中遇到的問題。
上一篇c 連接 oracle
下一篇c 讀取oracle代碼