Manual configuration in OCI for Windows

Source: Internet
Author: User

Http://www.cppblog.com/Khan/archive/2010/06/02/110361.html

The company's project was originally processed in Linux. due to recent staff transfers. you need to add several new users. in order to allow new users who are not familiar with the Linux development environment to get started with the company's basic library as soon as possible, I undertook the work of building a development environment in windows.

The first one is to compile the OCI-based encapsulation ocicpp used by the company into a mingw32 version.

1. Necessary tools

Instant Client downloads
For Microsoft Windows

Http://www.oracle.com/technology/software/tech/oci/instantclient/htdocs/winsoft.html

Oracle Instant Client

Mingw32

Self-search, I use 3.42
Win32 version of GCC

Pexports for mingw32

Self-Search
Export the DLL of C as def. If your mingw does not come with it, download it and place it in a path that can be found. c ++ DLL is not supported.

Install the above tools on your own
Decompress instantclient to D:/develop/cplus/instantclient_11_1/
Install mingw32 on D:/develop/cplus/mingw/
Extract pexports.exe to D:/develop/cplus/mingw/bin/

2. Generate OCI lib that can be called by mingw32
Because the official Oracle oci sdk does not provide mingw link packages, We need to manually create a package based on the DLL.

Use DLL to generate lib of mingw32
Export def
D:/develop/cplus/instantclient_11_1/SDK/lib> pexports OCI. dll> OCI. Def

Generate lib
D:/develop/cplus/instantclient_11_1/SDK/lib> dlltool -- dllname OCI. dll -- def OCI. def -- output-lib liboci.

In this way, we have a liboci. A that can be mingw32 link.

3. Configure the Oracle Instant Client
Only three environment variables are required.
Tns_admin = D:/develop/cplus/instantclient_11_1
Nls_lang = simplified chinese_china.zhs16gbk
Path = % PATH %; % tns_admin %/

4. Compile and execute the test program.
I cannot disclose the source code of ocicpp due to the confidentiality agreement.
However, the test program based on ocicpp can be pasted.

# Include <unistd. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <iostream>
# Include "ocicpp/ocicpp. H"
# Include "ocicpp/DB. H"

Using namespace ocicpp;

Bool insertdatabysql (connection * pconn)
{
Cursor cursor;
String strsql;

Strsql = "insert into testocicpp (testid, username, password, address) values (13, 'xxxxx ','! @ #$1234 ', 'xxxxx ')";

Try {
Pconn-> execquery (strsql, cursor );
Cursor. Drop ();
Pconn-> transcommit ();
}
Catch (oraerror ERR ){
Cout <"insertdatabysql exception:" <err. Message <Endl;
Return false;
}
Return true;
}

Bool updatedatabysql (connection * pconn)
{
Cursor cursor;
String strsql;

Strsql = "Update testocicpp SET Password = '& * () 7890 '";

Try {
Pconn-> execquery (strsql, cursor );
Cursor. Drop ();
Pconn-> transcommit ();
}
Catch (oraerror ERR ){
Cout <"updatedatabysql exception:" <err. Message <Endl;
Return false;
}
Return true;
}

Bool delrecordbysql (connection * pconn)
{
Cursor cursor;
String strsql;

Strsql = "delete from testocicpp ";

Try {
Pconn-> execquery (strsql, cursor );
Cursor. Drop ();
Pconn-> transcommit ();
}
Catch (oraerror ERR ){
Cout <"delrecordbysql exception:" <err. Message <Endl;
Return false;
}
Return true;
}
Bool fetchdatabysql (connection * pconn)
{
Cursor cursor;
String strsql;

Strsql = "select memberid, mobile, passwd, address from tblmember where rownum <20 ";

Try {
Pconn-> execquery (strsql, cursor );
While (cursor. Fetch ()){
Cout <"... record..." <Endl;
Cout <"testid:" <cursor. getint ("memberid") <Endl;
Cout <"username:" <cursor. getstr ("mobile") <Endl;
Cout <"Password:" <cursor. getstr ("passwd") <Endl;
Cout <"Address:" <cursor. getstr ("Address") <Endl;
}
Cursor. Drop ();
Pconn-> transcommit ();
}
Catch (oraerror ERR ){
Cout <"fetchdatabysql exception:" <err. Message <Endl;
Return false;
}
Return true;
}

Bool insertdatabyprocedure (connection * pconn)
{
Cursor cursor;
String strsql;
Int iretcode = 0;
Strsql = "begin testprocinsert (13, 'xxxxx ','! @ #$1234 ', 'xxxxx',: retcode); end ;";

Try {
Pconn-> prepare (strsql, cursor );
Cursor. BIND (": retcode", iretcode );
Cursor.exe cute (); cursor. Drop ();
}
Catch (oraerror ERR ){
Cout <"insertdatabysql exception:" <err. Message <Endl;
Return false;
}
Cout <"insertdatabyprocedure retcode:" <iretcode <Endl;

Return true;
}

Bool updatedatabyprocedure (connection * pconn)
{

Return false;
}

Bool delrecordbyprocedure (connection * pconn)
{
Return false;
}

Bool fetchdatabyprocedure (connection * pconn)
{

Return false;
}

Int startrun (INT p_imode)
{
Connection dbconn;

Ocicpp: DB: Init ();

Try {
Ocicpp: DB: connect ("smsdb", "point", "point1234", dbconn );

Switch (p_imode)
{
Case 1:
Insertdatabysql (& dbconn );
Break;
Case 2:
Updatedatabysql (& dbconn );
Break;
Case 3:
Delrecordbysql (& dbconn );
Break;
Case 4:
Fetchdatabysql (& dbconn );
Break;
Case 5:
Insertdatabyprocedure (& dbconn );
Break;
Case 6:
Updatedatabyprocedure (& dbconn );
Break;
Case 7:
Delrecordbyprocedure (& dbconn );
Break;
Case 8:
Fetchdatabyprocedure (& dbconn );
Break;
Default:
Break;
}
} Catch (oraerror ERR ){
Cout <"oraerror exception:" <err. Message <Endl;
}
Return 0;
}

Int main (INT argc, char * argv [])
{
If (argc! = 2)
{
Cout <Endl;
Cout <"Please input run mode (1-6):" <Endl;
Cout <Endl;
Cout <"1: insertdatabysql" <Endl;
Cout <"2: updatedatabysql" <Endl;
Cout <"3: delrecordbysql" <Endl;
Cout <"4: fetchdatabysql" <Endl;
Cout <"5: insertdatabyprocedure" <Endl;
Cout <"6: updatedatabyprocedure" <Endl;
Cout <"7: delrecordbyprocedure" <Endl;
Cout <"8: fetchdatabyprocedure" <Endl;
Cout <Endl;
Exit (-1 );
}
Int imode = atoi (argv [1]);

// Pid_t ipid;

// Ipid = fork ();

// If (ipid = 0)
//{
Startrun (imode );
Exit (0 );
//}
// Else if (ipid <0)
//{
// Cout <"Fork failure" <Endl;
//}
// Sleep (3 );
Cout <"................. finish ................. "<Endl;
Return 0;
}

/*************************************** **************************
Create Table testocicpp
(
Testid number (10, 0) default 1 not null,
Username varchar2 (16) default ''not null,
Password varchar2 (16) default ''not null,
Address varchar2 (128) default ''not null
) Tablespace rism_db;

Create or replace procedure testprocinsert (
P_itestid in number, p_susername in string, p_spassword in string,
P_saddress in string, p_iretcode out number)
Is
Begin
P_iretcode: = 0;
Insert into testocicpp (testid, username, password, address)
Values (p_itestid, p_susername, p_spassword, p_saddress );
Commit;

Exception
When others then
P_iretcode: = sqlcode;
End;
/

**************************************** *************************/

Compiled into ocicpp.exe

Two dllfiles and ocicpp.exe are in the same directory.
Find oraociei11.dll OCI. dll in the installation directory of Oracle Instant Client
The file name may vary depending on the version. oraociei11.dll.
You can also throw the two DLL files to the System32 directory.

My server is RHEL 3.4 and the database environment is 9.2.04.
Client environment for Windows mingw32-gcc3.42

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.