/************************************************************************/
/************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include "DCI.h"
OCIEnv *envhp;
OCISvcCtx *svchp;
OCIServer *srvhp;
OCISession *authp;
OCIStmt *stmthp;
OCIDescribe *dschp;
OCIError *errhp;
OCIDefine *defhp[3];
OCIBind *bidhp [4];
sb2 ind[3];
text szpersonid[11];
text szsex[2];
text szname[51];
text szemail[51];
text szphone[26];
char sql[256];
int main(int argc, char *argv[])
{
char strServerName[50];
char strUserName[50];
char strPassword[50];
strcpy(strServerName,"localhost:5236");
strcpy(strUserName,"SYSDBA");
strcpy(strPassword,"SYSDBA");
OCIInitialize(OCI_DEFAULT, NULL, NULL, NULL, NULL);
OCIEnvInit(&envhp, OCI_DEFAULT,0, 0);
OCIHandleAlloc(envhp, (dvoid**)&svchp, OCI_HTYPE_SVCCTX, 0, 0);
OCIHandleAlloc(envhp, (dvoid**)&srvhp, OCI_HTYPE_SERVER, 0, 0);
OCIHandleAlloc(envhp, (dvoid**)&authp, OCI_HTYPE_SESSION, 0, 0);
OCIHandleAlloc(envhp, (dvoid**)&errhp, OCI_HTYPE_ERROR, 0, 0);
OCIHandleAlloc(envhp, (dvoid**)&dschp, OCI_HTYPE_DESCRIBE,0,0);
OCIServerAttach(srvhp, errhp,(text *)strServerName, (sb4)strlen(strServerName),OCI_DEFAULT);
OCIAttrSet(authp,OCI_HTYPE_SESSION,(text *)strUserName, (ub4)strlen(strUserName),OCI_ATTR_USERNAME,errhp);
OCIAttrSet(authp,OCI_HTYPE_SESSION,(text *)strPassword, (ub4)strlen(strPassword), OCI_ATTR_PASSWORD,errhp);
OCIAttrSet ((dvoid*)svchp, (ub4) OCI_HTYPE_SVCCTX, (dvoid*)srvhp, (ub4) 0, OCI_ATTR_SERVER, errhp);
OCIAttrSet(svchp, OCI_HTYPE_SVCCTX,(dvoid*)authp, 0, OCI_ATTR_SESSION, errhp);
OCISessionBegin (svchp, errhp, authp,OCI_CRED_RDBMS,OCI_DEFAULT);
OCIHandleAlloc(envhp, (dvoid**)&stmthp,OCI_HTYPE_STMT, 0, 0);
/************************************************************************/
/************************************************************************/
strcpy(sql, "select personid, name, phone from person.person;");
OCIStmtPrepare(stmthp, errhp,(text *)sql, strlen(sql),OCI_NTV_SYNTAX, OCI_DEFAULT);
OCIDefineByPos (stmthp,&defhp[0],errhp, 1,(ub1*)szpersonid, sizeof(szpersonid),SQLT_STR,&ind[0], 0, 0, OCI_DEFAULT);
OCIDefineByPos (stmthp,&defhp[1],errhp, 2,(ub1*)szname, sizeof(szname),SQLT_STR,&ind[1], 0, 0, OCI_DEFAULT);
OCIDefineByPos (stmthp,&defhp[2],errhp, 3,(ub1*)szphone, sizeof(szphone),SQLT_STR,&ind[2], 0, 0, OCI_DEFAULT);
OCIStmtExecute(svchp, stmthp,errhp, (ub4)0, 0, NULL, NULL, OCI_DEFAULT);
printf("%-10s%-10s%-10s
", "PERSONID", "NAME", "PHONE");
while((OCIStmtFetch(stmthp, errhp,1,OCI_FETCH_NEXT,OCI_DEFAULT))!=OCI_NO_DATA)
{
printf("%-10s", szpersonid);
printf("%-10s", szname);
printf("%-10s
", szphone);
}
/************************************************************************/
/************************************************************************/
memset(sql, 0, sizeof(sql));
strcpy(sql, "insert into person.person(sex, name, email, phone) values(:sex,:name,:email,:phone);");
OCIStmtPrepare(stmthp, errhp,(text *)sql, strlen(sql),OCI_NTV_SYNTAX, OCI_DEFAULT);
OCIBindByName (stmthp, &bidhp[0], errhp, ":sex", 4, szsex, sizeof(szsex), SQLT_AFC, NULL, NULL, NULL, 0, NULL, 0);
OCIBindByName (stmthp, &bidhp[1], errhp, ":name", 5, szname, sizeof(szname), SQLT_AFC, NULL, NULL, NULL, 0, NULL, 0);
OCIBindByName (stmthp, &bidhp[2], errhp, ":email", 6, szemail, sizeof(szemail), SQLT_AFC, NULL, NULL, NULL, 0, NULL, 0);
OCIBindByName (stmthp, &bidhp[3], errhp, ":phone", 6, szphone, sizeof(szphone), SQLT_AFC, NULL, NULL, NULL, 0, NULL, 0);
memset(szsex, 0, sizeof(szsex));
strcpy(szsex, "M");
memset(szname, 0, sizeof(szname));
strcpy(szname, "张三");
memset(szemail, 0, sizeof(szemail));
strcpy(szemail, "zhangsan@dameng.com");
memset(szphone, 0, sizeof(szphone));
strcpy(szphone, "027-87588000");
OCIStmtExecute(svchp, stmthp, errhp, (ub4)0, (ub4) 0, (CONST OCISnapshot*) 0, (OCISnapshot*) 0, (ub4) OCI_DEFAULT);
OCITransCommit(svchp, errhp, OCI_DEFAULT);
/************************************************************************/
/************************************************************************/
memset(sql, 0, sizeof(sql));
strcpy(sql, "update person.person set sex='M',name='Liuhuan',email='liujian@mail',phone='13636396811' WHERE personid=1");
OCIStmtPrepare(stmthp, errhp,(text *)sql, strlen(sql),OCI_NTV_SYNTAX, OCI_DEFAULT);
OCIStmtExecute(svchp, stmthp, errhp, (ub4)0, (ub4) 0, (CONST OCISnapshot*) 0, (OCISnapshot*) 0, (ub4) OCI_DEFAULT);
OCITransCommit(svchp, errhp, OCI_DEFAULT);
/************************************************************************/
/************************************************************************/
memset(sql, 0, sizeof(sql));
strcpy(sql, "delete from person.person WHERE personid=?");
OCIStmtPrepare(stmthp, errhp,(text *)sql, strlen(sql),OCI_NTV_SYNTAX, OCI_DEFAULT);
memset(szpersonid, 0, sizeof(szpersonid));
strcpy(szpersonid, "20");
OCIBindByPos(stmthp, &bidhp[0], errhp, 1, szpersonid, sizeof(szpersonid), SQLT_AFC, NULL, NULL, NULL, 0, NULL, 0);
OCIStmtExecute(svchp, stmthp, errhp, (ub4)0, (ub4) 0, (CONST OCISnapshot*) 0, (OCISnapshot*) 0, (ub4) OCI_DEFAULT);
OCITransCommit(svchp, errhp, OCI_DEFAULT);
OCISessionEnd(svchp, errhp, authp, (ub4) 0);
OCIServerDetach(srvhp, errhp, OCI_DEFAULT);
OCIHandleFree((dvoid*)dschp, OCI_HTYPE_DESCRIBE);
OCIHandleFree((dvoid*)stmthp, OCI_HTYPE_STMT );
OCIHandleFree((dvoid*)errhp, OCI_HTYPE_ERROR);
OCIHandleFree((dvoid*)authp, OCI_HTYPE_SESSION );
OCIHandleFree((dvoid*)svchp, OCI_HTYPE_SVCCTX);
OCIHandleFree((dvoid*)srvhp, OCI_HTYPE_SERVER);
int errcode = 0;
char errbuf[512] = {0};
OCIErrorGet(errhp, 1, (OraText*)NULL, &errcode, (ub1*)errbuf, sizeof(errbuf), OCI_HTYPE_ERROR);
return 0;
}
# gcc dmdci.c -ldmoci -L.