In the previous section we talked about ADO connecting to Oracle, which we tried to connect to the Oracle database under the Windows platform in a Occi way, and the next section discussed connecting the remote Oracle database in a Linux environment by OCCI.
First of all, Oracle C + + callinterface (OCCI) is a set of Oracle's own application programming interfaces that allow C + + programs to interact with one or more Oracle databases, although the OCI is much more difficult to develop, but it is extremely fast, It is also an underlying interface that can manipulate almost any object in an Oracle database.
Environment: Windows7 64bit
Ide:vs 2010
Service side: winserver2008 Oracle 12c
Download the development package:
First download the corresponding Cilent development package on the Oracle website, which contains some necessary library files. Be sure to download the relative version of the client development package. I put my development kit under the D-Drive.
http://download.csdn.net/detail/u012139536/9561994
Occi to access the Oracle database, a few parts of the file: include header files, Lib library files, dll files, in fact, the basic steps are how to find these files.
To get these files, you need to download several packages on the Oracle website:
1:Instant Client package-basic: All files required to run OCI, OCCI, and JDBC-OCI applications
2:Instant Client package-sdk: Additional header files and an example makefile for developing Oracle application S with Instant Client
3:occi interface: Oracle C + + call Interface
Download each of these three packages separately:
1:instant client Package-basic, Instant client package-sdk Two-pack: http://www.oracle.com/technetwork/topics/ Winsoft-085727.html (Other platforms: http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html)
With a version selected, the selected version needs to be higher than the version of the Oracle database, and the versions of the two packages are consistent.
2:occi Interface: http://www.oracle.com/technetwork/database/occidownloads-083553.html
Select a version to match the version of the first two packages. As can be seen from the table in this download page, VS2010 provides a zip package, whereas the previous VS version requires an Oracle client to access the database, so it's much easier to use vs2010, just the appropriate library file.
When the download is complete:
Unzip each of the three packages:
1: Required include header file under Instantclient-sdk-nt-11.2.0.4.0\instantclient_11_2\sdk\include path (Instant Client PACKAGE-SDK package)
The 2:lib file is in the Occi interface package: Oraocci11.lib or Oraocci11d.lib (the Lib file is also available in the SDK package, but the test discovery is not used)
3:dll files in the Occi interface package: Oraocci11.dll or Oraocci11d.dll, and instant Client Package-basic package: Oraociei11.dll, Oci.dll, Orannzsbb11.dll (if Orannzsbb11 not also can, the package of other DLLs, sym, EXE files without the tube, as to the specific role has not yet been studied).
Set Environment variables:
Then set the system variable in the environment variable, advanced system settings, right-click Properties, Computer
Path=d:\instantclient-basic-nt-12.1.0.2.0\instantclient_12_1
Nls_lang=american_america. Zhs16gbk
Configuration VS2010:
Create an empty project first occiconnectoracle
1. Add Library file directory:
Solution Right-occiconnectoracle-> property->c/c++-> General-, add-in directory, inbound directory address: D:\instantclient-sdk-nt-12.1.0.2.0\ Instantclient_12_1\sdk\include
2. Add header file directory:
Additional Library directories, general-----linker, properties, and more: D:\INSTANTCLIENT-SDK-NT-12.1.0.2.0\INSTANTCLIENT_12_1\SDK\LIB\MSVC\VC10
3. Add a library file:
Properties, linker, input, Additional dependencies: Here are oraocci12.lib and oraocci12d.lib two files, which are used when Oraocci12.lib is in release mode and oraocci12d.lib for debug mode.
Code section:
- /*************************** uses Oracle itself to provide a occi way to operate the database *******************/
- The purpose of the code is to verify that the Oracle header and Lib file paths in makefile are correct.
- #include <iostream>
- #define WIN32COMMON//Avoid function redefinition errors
- #include <occi.h>
- Using namespace std;
- Using namespace Oracle::occi;
- int main ()
- {
- System ("pause");
- //Create OCCI Context environment
- Environment *env = environment::createenvironment ();
- if (NULL = = env) {
- printf ("Createenvironment error.\n");
- return-1;
- }
- Else
- cout << "Success" << Endl;
- String name = "System";
- String pass = "123";
- String srvname = "192.168.26.74:1521/ORCL";
- Try
- {
- //Create a database connection
- Connection *conn = env->createconnection (name, pass, srvname); //username, password, database name
- if (NULL = = conn) {
- printf ("CreateConnection error.\n");
- return-1;
- }
- Else
- cout << "conn Success" << Endl;
- Data manipulation, creating statement objects
- Statement *pstmt = NULL; //Statement Object
- pstmt = Conn->createstatement ();
- if (NULL = = pstmt) {
- printf ("createstatement error.\n");
- return-1;
- }
- //Query database time
- Std::string strtemp;
- ResultSet *prs = Pstmt->executequery (
- "Select To_char (sysdate, ' Yyyy-mm-dd HH24:MI:SS ') from DUAL");
- While (Prs->next ()) {
- strtemp = prs->getstring (1);
- printf ("db time:%s.\n", Strtemp.c_str ());
- //int type value with Getint ()
- Break ;
- }
- Pstmt->closeresultset (PRs);
- //--------inserting---------
- //Specify DML as auto-commit
- Pstmt->setautocommit (TRUE);
- //Set SQL statement to execute
- //pstmt->setsql ("INSERT into TA (ID, NAME) VALUES (1, ' ZS ')");
- Pstmt->setsql ("INSERT into Table_test_wang (NAME, NUM, age) VALUES (' Deng Chao ', ' 99 ', ' 41 ')");
- //Execute SQL statement
- unsigned int nret = Pstmt->executeupdate ();
- if (nret = = 0) {
- printf ("executeupdate insert error.\n");
- }
- //Terminate statement object
- Conn->terminatestatement (PSTMT);
- Close connection
- Env->terminateconnection (conn);
- //Penv->terminateconnection (Pconn);
- }
- catch (SQLException e)
- {
- cout << e.what () << Endl;
- System ("pause");
- return-1;
- }
- Releasing the OCCI context environment
- Environment::terminateenvironment (env);
- cout << "end!" << Endl;
- System ("pause");
- return 0;
- }
:
The program displays the upload data successfully, and uses SQL developer to view the database data
You can see that the database already exists and the upload is successful.
C + + Connect Oracle's OCCI (Windows)