When developing software, it is often necessary to operate the database. The stored procedures of Oracle database are one of the very important functions. Through stored procedures, we can encapsulate some common business logic into a reusable code library, simplifying the development and maintenance of the code. This article will introduce how to use C language to call the stored procedure of Oracle database.
Before you start writing C programs, you need to install the Oracle database and Oracle client. If you have already installed the Oracle client, you can skip this step.
Before installing the Oracle client, you need to install the Oracle database and create a database user for testing. You can download the Oracle client from Oracle's official website and select the version that matches your operating system version for installation.
The following is a sample code that uses C language to call an Oracle stored procedure, which queries the specified records in the user table.
#include <stdio.h> #include <stdlib.h> #include <oci.h> void report_error(OCIError *errhp) { text msgbuf[512]; sb4 errcode = 0; OCIErrorGet((dvoid *) errhp, (ub4) 1, (text *) NULL, &errcode, msgbuf, (ub4) sizeof (msgbuf), OCI_HTYPE_ERROR); fprintf(stderr, "Error code %d, msg: %s\n", errcode, msgbuf); exit(EXIT_FAILURE); } int main() { OCIEnv *envhp; OCIError *errhp; OCIServer *srvhp; OCISession *authp; OCIStmt *stmthp; OCIParam *paramhp; OCIParam *paramhp2; OCIParam *paramhp3; ub4 pos = 0; text *username = (text *) "YOUR_USERNAME"; text *password = (text *) "YOUR_PASSWORD"; text *db = (text *) "YOUR_DATABASE"; text *proc_name = (text *) "YOUR_PROC_NAME"; int user_id = 1; text *name = (text *) malloc(512 * sizeof (text)); sb4 name_len = 0; OCIInitialize((ub4) OCI_DEFAULT, (dvoid *)0, (dvoid * (*)(dvoid *, size_t))0, (dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *))0 ); 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); OCIHandleAlloc((dvoid *) envhp, (dvoid **)&authp, OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0); OCIHandleAlloc((dvoid *) envhp, (dvoid **) &stmthp, OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0); OCIAttrSet((dvoid *) srvhp, OCI_HTYPE_SERVER, (dvoid *) db, (ub4) strlen((char *) db), OCI_ATTR_SERVER, errhp); OCIServerAttach(srvhp, errhp, (text *) 0, (sb4) 0, OCI_DEFAULT); OCIAttrSet((dvoid *) authp, OCI_HTYPE_SESSION, (dvoid *) username, (ub4) strlen((char *) username), OCI_ATTR_USERNAME, errhp); OCIAttrSet((dvoid *) authp, OCI_HTYPE_SESSION, (dvoid *) password, (ub4) strlen((char *) password), OCI_ATTR_PASSWORD, errhp); OCISessionBegin(srvhp, errhp, authp, OCI_CRED_RDBMS, OCI_DEFAULT); OCIAttrSet((dvoid *) stmthp, OCI_HTYPE_STMT, (dvoid *) proc_name, (ub4) strlen((char *) proc_name), OCI_ATTR_PROCEDURE_NAME, errhp); OCIStmtPrepare(stmthp, errhp, (text *) "begin YOUR_PACKAGE.YOUR_PROC(:1,:2,:3); end;", (ub4) strlen("begin YOUR_PACKAGE.YOUR_PROC(:1,:2,:3); end;"), OCI_NTV_SYNTAX, OCI_DEFAULT); OCIParamGet(stmthp, OCI_HTYPE_STMT, errhp, (dvoid **)¶mhp, (ub4)1); OCIDefineByPos(stmthp, ¶mhp3, errhp, 3, (dvoid *)&name, (sb4) sizeof(name), SQLT_STR, (dvoid *)&name_len, NULL, OCI_DEFAULT); OCIParamGet(stmthp, OCI_HTYPE_STMT, errhp, (dvoid **)¶mhp2, (ub4)2); OCIBindByPos(stmthp, ¶mhp2, errhp, 2, (dvoid*)&user_id, (sb4)sizeof(user_id), SQLT_INT, (dvoid *)0, (ub2 *)0, (ub2 *)0, 0, (ub4 *)0, OCI_DEFAULT); OCIStmtExecute(authp, stmthp, errhp, (ub4) 0, (ub4) 0, (OCISnapshot *)0, (OCISnapshot *)0, OCI_DEFAULT); printf("user name: %s\n", name); OCIHandleFree((dvoid *) stmthp, OCI_HTYPE_STMT); OCIHandleFree((dvoid *) authp, OCI_HTYPE_SESSION); OCIHandleFree((dvoid *) srvhp, OCI_HTYPE_SERVER); OCIHandleFree((dvoid *) errhp, OCI_HTYPE_ERROR); OCIHandleFree((dvoid *) envhp, OCI_HTYPE_ENV); return 0; }
After writing the C code, we need to compile and run the code to test whether the stored procedure can be successfully called.
The command to compile C code is as follows:
gcc -o demo demo.c -I$ORACLE_HOME/include -L$ORACLE_HOME/lib -lclntsh
where $ORACLE_HOME is the installation path of the Oracle client.
For compilation on Windows platform, -lclntsh
needs to be changed to -locci
.
This article introduces how to use C language to call the stored procedure of Oracle database. It should be noted that the installation path of the Oracle client needs to be set up, and the compilation of the C program needs to link to the correct library file. In actual development work, you also need to pay attention to the writing of stored procedures and security processing.
The above is the detailed content of How to call the stored procedure of Oracle database using C language. For more information, please follow other related articles on the PHP Chinese website!