Home > Backend Development > C++ > Using Oracle database in C++ and its sample code

Using Oracle database in C++ and its sample code

王林
Release: 2023-08-22 15:57:33
Original
1920 people have browsed it

Using Oracle database in C++ and its sample code

Oracle is a powerful relational database management system. Using Oracle database in C can help us manage the database more efficiently. This article will introduce how to use Oracle database in C and related sample code.

1. Install and configure the Oracle database driver

Before using the Oracle database, you need to install the corresponding Oracle driver. Oracle officially provides ODBC drivers, which we can download and install from the official website.

After the installation is complete, go to Control Panel -> Management Tools -> Data Source (ODBC). You can see the ODBC driver in "Drivers". We need to select the corresponding driver and configure it.

Enter the alias of the Oracle database to be connected in "Data Source Name", such as "orcl". Enter the host name or IP address to connect to the database in "Server Name". Enter the username to connect to the database in "User ID" and enter the corresponding password in Password. Click the "Test Connection" button. If it prompts "Connection successful", the connection is successful.

2. Using Oracle database

To use Oracle database in C, you need to introduce the oracle header file and make relevant settings.

1. Header file inclusion

First, you need to include the header files oci.h and oci.cpp. These two header files are in the OCI/include directory under the Oracle installation directory.

2. Link Oracle library files

You need to link Oracle library files, including oci.lib, ociw32.lib and orannzsbb11.dll, etc.

3. Create an OCI handle

OCI handle is a memory management mechanism provided by Oracle. The OCI handle is used to manage the connection and interaction between the OCILIB library and Oracle services (SQL statements).

When using the OCI handle, you need to apply for it first and then configure it as needed. Commonly used OCI handles include:
(1) Environment handle: OCIEnv.
(2) Service handle: OCISvcCtx.
(3) Session handle: OCISession.
(4) Statement handle: OCIStmt.
(5) Result set handle: OCIDefine, OCIParam.

The following is the code to apply for OCI environment handle, service handle, session handle and statement handle:

OCIEnv* envhp;
OCIStmt* stmthp;
OCIServer* srvhp;
OCISession* sesshp;

//申请环境句柄
int err = OCIEnvCreate(&envhp, OCI_THREADED|OCI_OBJECT, (dvoid*)0, (dvoid*(*)(dvoid*, size_t))0, (dvoid*(*)(dvoid*, dvoid*, size_t))0, (void(*)(dvoid*, dvoid*))0, (size_t)0, (dvoid**)0);

//申请服务句柄
err = OCIHandleAlloc(envhp, (dvoid**)&srvhp, OCI_HTYPE_SERVER, (size_t)0, (dvoid**)0);

//申请会话句柄
err = OCIHandleAlloc(envhp, (dvoid**)&sesshp, OCI_HTYPE_SESSION, (size_t)0, (dvoid**)0);

//申请语句句柄
err = OCIHandleAlloc(envhp, (dvoid**)&stmthp, OCI_HTYPE_STMT, (size_t)0, (dvoid**)0);
Copy after login

4. Connect to Oracle database

You need to set the service name of OCIServer first. Then call the OCILogon2 function to connect to the Oracle database. The following is the code to connect to the Oracle database:

//设置服务名称
const char* db_name = "orcl";
err = OCIAttrSet(srvhp, OCI_HTYPE_SERVER, (dvoid*)db_name, strlen(db_name), OCI_ATTR_SERVER_NAME, envhp);

//连接数据库
const char* user_name = "scott";//连接的用户名
const char* password = "tiger";//连接的密码
const char* dblink = 0;//连接方式
ub4 dblink_len = 0;
err = OCILogon2(envhp, errhp, &sesshp, (CONST OraText*)user_name, strlen(user_name), (CONST OraText*)password, strlen(password), (CONST OraText*)dblink, (ub4)dblink_len, (CONST OraText*)0, 0);
Copy after login

5. Execute SQL statements

You need to use the OCIStmt handle and the OCIDefine handle to execute the SQL statement. The OCIStmt handle is used to prepare SQL statements, and the OCIDefine handle is used to define output variables.
The following is the code to execute the SQL statement:

//定义SQL语句
const char* sql = "select empno from emp where empno=:1";
err = OCIStmtPrepare(stmthp, errhp, (CONST OraText*)sql, strlen(sql), OCI_NTV_SYNTAX, OCI_DEFAULT);

//绑定输入参数
ub2 empno = 7900;
err = OCIBindByPos(stmthp, &bindhp, errhp, 1, &empno, sizeof(empno), SQLT_INT, 0, 0, 0, 0, 0, OCI_DEFAULT);

//执行SQL语句
err = OCIStmtExecute(svchp, stmthp, errhp, 1, 0, 0, 0, OCI_DEFAULT);

//定义输出变量
ub2 out_empno;
ub2 ind;
sb2 out_len;
ERRCALL(OCIDefineByPos(stmthp, &dfnhp, errhp, 1, (dvoid*)&out_empno, sizeof(out_empno), SQLT_INT, &ind, &out_len, 0, OCI_DEFAULT));

//获取结果
err = OCIStmtFetch(stmthp, errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
Copy after login

3. Sample code

The following is the complete code to connect to the Oracle database and execute the SQL statement:

#include<iostream>
#include<occi.h>
#include<oci.h>

using namespace std;
using namespace oracle::occi;

int main()
{
    OCIEnv *envhp;
    OCIStmt *stmthp;
    OCIServer *srvhp;
    OCISession *sesshp; 

    // 申请OCI环境句柄
    int err = OCIEnvCreate(&envhp, OCI_THREADED|OCI_OBJECT, (dvoid*)0, (dvoid*(*)(dvoid*, size_t))0, (dvoid*(*)(dvoid*, dvoid*, size_t))0, (void(*)(dvoid*, dvoid*))0, (size_t)0, (dvoid**)0);

    // 申请服务句柄
    err = OCIHandleAlloc(envhp, (dvoid**)&srvhp, OCI_HTYPE_SERVER, (size_t)0, (dvoid**)0);

    // 申请会话句柄
    err = OCIHandleAlloc(envhp, (dvoid**)&sesshp, OCI_HTYPE_SESSION, (size_t)0, (dvoid**)0);

    // 申请语句句柄
    err = OCIHandleAlloc(envhp, (dvoid**)&stmthp, OCI_HTYPE_STMT, (size_t)0, (dvoid**)0);

    // 设置服务名称
    const char* db_name = "orcl";
    err = OCIAttrSet(srvhp, OCI_HTYPE_SERVER, (dvoid*)db_name, strlen(db_name), OCI_ATTR_SERVER_NAME, envhp);

    // 连接数据库
    const char* user_name = "scott";//连接的用户名
    const char* password = "tiger";//连接的密码
    const char* dblink = 0;//连接方式
    ub4 dblink_len = 0;
    err = OCILogon2(envhp, errhp, &sesshp, (CONST OraText*)user_name, strlen(user_name), (CONST OraText*)password, strlen(password), (CONST OraText*)dblink, (ub4)dblink_len, (CONST OraText*)0, 0);

    // 执行SQL语句
    const char* sql = "select empno from emp where empno=:1";
    err = OCIStmtPrepare(stmthp, errhp, (CONST OraText*)sql, strlen(sql), OCI_NTV_SYNTAX, OCI_DEFAULT);

    ub2 empno = 7900;// 输入的empno
    OCIBind* bindhp;
    err = OCIBindByPos(stmthp, &bindhp, errhp, 1, &empno, sizeof(empno), SQLT_INT, 0, 0, 0, 0, 0, OCI_DEFAULT);

    OCIDefine* dfnhp;
    ub2 out_empno;
    ub2 ind;
    sb2 out_len;
    ERRCALL(OCIDefineByPos(stmthp, &dfnhp, errhp, 1, (dvoid*)&out_empno, sizeof(out_empno), SQLT_INT, &ind, &out_len, 0, OCI_DEFAULT));

    err = OCIStmtExecute(svchp, stmthp, errhp, 1, 0, 0, 0, OCI_DEFAULT);

    err = OCIStmtFetch(stmthp, errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
    while(err == OCI_SUCCESS)
    {
        cout << out_empno << endl;
        err = OCIStmtFetch(stmthp, errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
    }

    // 断开连接
    OCILogoff(sesshp, envhp); 
    OCIHandleFree(srvhp, OCI_HTYPE_SERVER);
    OCIHandleFree(sesshp, OCI_HTYPE_SESSION); 
    OCIHandleFree(stmthp, OCI_HTYPE_STMT); 
    OCIHandleFree(envhp, OCI_HTYPE_ENV); 

    return 0;
}
Copy after login

That’s it Using Oracle database and its sample code in C, I hope it will be helpful to everyone.

The above is the detailed content of Using Oracle database in C++ and its sample code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template