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);
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);
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);
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; }
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!

The main differences between C# and C are memory management, polymorphism implementation and performance optimization. 1) C# uses a garbage collector to automatically manage memory, while C needs to be managed manually. 2) C# realizes polymorphism through interfaces and virtual methods, and C uses virtual functions and pure virtual functions. 3) The performance optimization of C# depends on structure and parallel programming, while C is implemented through inline functions and multithreading.

The DOM and SAX methods can be used to parse XML data in C. 1) DOM parsing loads XML into memory, suitable for small files, but may take up a lot of memory. 2) SAX parsing is event-driven and is suitable for large files, but cannot be accessed randomly. Choosing the right method and optimizing the code can improve efficiency.

C is widely used in the fields of game development, embedded systems, financial transactions and scientific computing, due to its high performance and flexibility. 1) In game development, C is used for efficient graphics rendering and real-time computing. 2) In embedded systems, C's memory management and hardware control capabilities make it the first choice. 3) In the field of financial transactions, C's high performance meets the needs of real-time computing. 4) In scientific computing, C's efficient algorithm implementation and data processing capabilities are fully reflected.

C is not dead, but has flourished in many key areas: 1) game development, 2) system programming, 3) high-performance computing, 4) browsers and network applications, C is still the mainstream choice, showing its strong vitality and application scenarios.

The main differences between C# and C are syntax, memory management and performance: 1) C# syntax is modern, supports lambda and LINQ, and C retains C features and supports templates. 2) C# automatically manages memory, C needs to be managed manually. 3) C performance is better than C#, but C# performance is also being optimized.

You can use the TinyXML, Pugixml, or libxml2 libraries to process XML data in C. 1) Parse XML files: Use DOM or SAX methods, DOM is suitable for small files, and SAX is suitable for large files. 2) Generate XML file: convert the data structure into XML format and write to the file. Through these steps, XML data can be effectively managed and manipulated.

Working with XML data structures in C can use the TinyXML or pugixml library. 1) Use the pugixml library to parse and generate XML files. 2) Handle complex nested XML elements, such as book information. 3) Optimize XML processing code, and it is recommended to use efficient libraries and streaming parsing. Through these steps, XML data can be processed efficiently.

C still dominates performance optimization because its low-level memory management and efficient execution capabilities make it indispensable in game development, financial transaction systems and embedded systems. Specifically, it is manifested as: 1) In game development, C's low-level memory management and efficient execution capabilities make it the preferred language for game engine development; 2) In financial transaction systems, C's performance advantages ensure extremely low latency and high throughput; 3) In embedded systems, C's low-level memory management and efficient execution capabilities make it very popular in resource-constrained environments.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
