473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

oracle-c++

How to connect to oracle using C++ program and select some data from a
table?

Dec 10 '05 #1
6 4904
On 10 Dec 2005 05:48:04 -0800, "sarat" <sh*******@gmail.com> wrote:
How to connect to oracle using C++ program and select some data from a
table?


You have posted three duplicate messages; one would have been quite
sufficient.

In order to connect to any database, you will need to use either the
API that comes with that product, or a third-party middleware such as
ODBC or OLEDB which have a C++ binding.

Have you tried:
comp.databases.oracle?
Google?
The Oracle website?

--
Bob Hairgrove
No**********@Home.com
Dec 10 '05 #2

Bob Hairgrove wrote:
You have posted three duplicate


Hmm, does that mean six?

Triplicate :-)

Gavin Deane

Dec 10 '05 #3
Hi,

You can connect to oracle either using ODBC (typically from a windows PC) or
using OCI (Oracle Call Interface). Look for the OCI manual on the oracle
site.


For instance:
////////////////////////////////////////////////////////////

// OCIConnection.cc

//-----------------------------------------------------------

// Author : Ron AF Greve

// Date : 20011104

// EMail : software THESIGN moonlit xs4all nl

// CopyRight 2001 by Ron AF Greve

//-----------------------------------------------------------

// Function : Connection to the database

//

////////////////////////////////////////////////////////////

static char rcsid[] = "$Id: OCIConnection.cc,v 1.7 2002/03/05 09:26:44
rgreve Exp rgreve $";

#include "OCIConnection.h"

#include <iostream>

bool COCIConnection::OCIInitialized = false;

COCIConnection::COCIConnection( const string& User, const string&
Password ):

User ( User ),

Password ( Password ),

HEnv ( 0 ),

HError ( 0 ),

HServer ( 0 ),

HService ( 0 ),

HAuth ( 0 ),

Attached ( false ),

Session ( false )

{

Init();

}

COCIConnection::COCIConnection( const string& Server, const string& User,
const string& Password ):

Server ( Server ),

User ( User ),

Password ( Password ),

HEnv ( 0 ),

HError ( 0 ),

HServer ( 0 ),

HService ( 0 ),

HAuth ( 0 ),

Attached ( false ),

Session ( false )

{

Init();

}

void COCIConnection::Init()

{

if( !OCIInitialized )

{

OCIInitialize( OCI_DEFAULT, 0, 0, 0, 0 );

OCIInitialized = true;

}

OCIEnvInit( &HEnv, OCI_DEFAULT, 0, 0 );

//OCIEnvCreate( &HEnv, OCI_DEFAULT, 0, 0, 0, 0, 0, 0 );

OCIHandleAlloc( reinterpret_cast<const dvoid *>( HEnv ),
reinterpret_cast<dvoid **>( &HService ), OCI_HTYPE_SVCCTX, 0, 0 );

OCIHandleAlloc( reinterpret_cast<const dvoid *>( HEnv ),
reinterpret_cast<dvoid **>( &HError ), OCI_HTYPE_ERROR, 0, 0 );

OCIHandleAlloc( reinterpret_cast<const dvoid *>( HEnv ),
reinterpret_cast<dvoid **>( &HServer ), OCI_HTYPE_SERVER, 0, 0 );

}

bool COCIConnection::Connect()

{

try

{

SetStatus( OCIServerAttach( HServer, HError, Server.size() ?
reinterpret_cast<const text *>( Server.c_str() ) : 0, Server.size(),
OCI_DEFAULT ) );

if( GetStatus() != OCI_SUCCESS )

{

cerr << __FILE__ << ":" << __LINE__ << " " << Report( HError ) << endl;

}

else

{

Attached = true;

// set attribute server context in the service context

SetStatus( OCIAttrSet( HService, OCI_HTYPE_SVCCTX, HServer, 0,
OCI_ATTR_SERVER, HError ) );

if( GetStatus() != OCI_SUCCESS )

{

cerr << __FILE__ << ":" << __LINE__ << " " << Report( HError ) << endl;

}

else

{

SetStatus( OCIHandleAlloc( reinterpret_cast<const dvoid *>( HEnv ),
reinterpret_cast<dvoid **>( &HAuth ), OCI_HTYPE_SESSION, 0, 0 ) );

if( GetStatus() != OCI_SUCCESS )

{

cerr << __FILE__ << ":" << __LINE__ << " " << Report( HError ) << endl;

}

else

{

OCIAttrSet( HAuth, OCI_HTYPE_SESSION, const_cast<dvoid *>(
reinterpret_cast<const dvoid *>( User.c_str() ) ), User.size(),
OCI_ATTR_USERNAME, HError );

OCIAttrSet( HAuth, OCI_HTYPE_SESSION, const_cast<dvoid *>(
reinterpret_cast<const dvoid *>( Password.c_str() ) ), Password.size(),
OCI_ATTR_PASSWORD, HError );

SetStatus( OCISessionBegin ( HService, HError, HAuth, OCI_CRED_RDBMS,
OCI_DEFAULT ) );

if( GetStatus() != OCI_SUCCESS )

{

cerr << __FILE__ << ":" << __LINE__ << " " << Report( HError ) << endl;

}

else

{

Session = true;

OCIAttrSet( HService, OCI_HTYPE_SVCCTX, HAuth, 0, OCI_ATTR_SESSION,
HError );

}

}

}

}

}

catch( ... ){}

return OCI_SUCCESS == GetStatus();

}

COCIStatement *COCIConnection::CreateStatement( const string& Statement )

{

COCIStatement *OCIStatement = 0;

if( OCI_SUCCESS == GetStatus() )

{

OCIStatement = new COCIStatement( HService, HEnv, HError, Statement );

}

return OCIStatement;

}

COCIConnection::~COCIConnection()

{

if( Session )

{

OCISessionEnd ( HService, HError, HAuth, OCI_DEFAULT );

}

if( Attached )

{

OCIServerDetach( HServer, HError, OCI_DEFAULT );

}

if( HServer )OCIHandleFree( HServer, OCI_HTYPE_SERVER );

if( HService )OCIHandleFree( HService, OCI_HTYPE_SVCCTX );

if( HAuth )OCIHandleFree( HAuth, OCI_HTYPE_SESSION );

if( HError )OCIHandleFree( HError, OCI_HTYPE_ERROR );

if( HEnv ) OCIHandleFree( HEnv, OCI_HTYPE_ENV );

// OCITerminate( OCI_DEFAULT );

}

//#define TEST

#ifdef TEST

#include <unistd.h>

int main( int ArgC, char *ArgV[] )

{

while( true )

{

COCIConnection *Connection = new COCIConnection( "", "RGREVE", "FRUTSN6" );

if( !Connection->Connect() )

{

cerr << "Fail" << endl;

}

else

{

char Name[ 200 ];

char Pct_Free[ 200 ];

int Percent = 30;

cin >> Percent;

memset( Name, 0, sizeof Name );

// COCIStatement *Statement = Connection->CreateStatement( "SELECT
TABLESPACE_NAME FROM DBA_FREE_SPACE" );

COCIStatement *Statement = Connection->CreateStatement(

"SELECT a.tablespace_name TSNAME, "

"SUM(a.sumb)*100/sum(a.tots) Pct_Free "

"FROM (SELECt tablespace_name, 0 tots, SUM(bytes) sumb, "

"MAX(bytes) largest, COUNT(*) chunks "

"FROM dba_free_space a "

"GROUP BY tablespace_name "

"UNION ALL "

"SELECT tablespace_name, SUM(bytes) tots, 0, 0, 0 "

"FROM dba_data_files "

"GROUP BY tablespace_name) a "

"group by a.tablespace_name "

"having SUM(a.sumb)*100/sum(a.tots) < :Percent"

);

Statement->DefineByPos( 1, Name, sizeof Name );

Statement->DefineByPos( 2, Pct_Free, sizeof Pct_Free );

Statement->BindByName( ":Percent", &Percent );

Statement->Execute();

while( Statement->Fetch() )

{

cerr << "Name = " << Name << " Pct_Free " << Pct_Free << endl;

}

delete Statement;

}

delete Connection;

Connection = 0;

sleep(1);

}

return 0;

}

#endif
--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"sarat" <sh*******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
How to connect to oracle using C++ program and select some data from a
table?

Dec 10 '05 #4
On 10 Dec 2005 06:13:10 -0800, de*********@hotmail.com wrote:

Bob Hairgrove wrote:
You have posted three duplicate
Hmm, does that mean six?


Possibly, but I only saw three. ;)
Triplicate :-)


One could also say: "He posted three messages, all duplicates of the
same message."

--
Bob Hairgrove
No**********@Home.com
Dec 10 '05 #5

"sarat" <sh*******@gmail.com> wrote in message news:11**********************@g44g2000cwa.googlegr oups.com...
How to connect to oracle using C++ program and select some data from a
table?


Look at C++-Wrapper around Oracle
* http://groups.google.com/groups?th=ff043117b474f920
* http://alexvn.freeservers.com/s1/download.html (Click on "C++-wrapper around Oracle ")
* http://www.codearchive.com/list.php?go=0705
* http://www.planet-source-code.com/vb...=3537&lngWId=3

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Dec 10 '05 #6
sarat wrote:
How to connect to oracle using C++ program and select some data from a
table?


In U++ (http://upp.sf.net):

#include <Oracle/Oracle7.h>

CONSOLE_APP_MAIN
{
Oracle7 oracle;
oracle.Open("scott/tiger@yorserver");
Sql sql(oracle);
sql.Execute("select ENAME, JOB from EMP");
while(sql.Fetch())
Cout() << String(sql[0]) << ": " << String(sql[1]);
}

Mirek
Dec 11 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jan Bols | last post by:
I've been trying to install Oracle 8.1.7 on a fresh Mandrake 9.1 O.S for days, but I'm still not able to get it running. I've tried several install instructions that I found on the internet but no...
4
by: susmita_ganguly | last post by:
Hi I am trying to upgrade from oracle 8i to oracle 9i on the same server ..I don't know much abt migration . Can anyone help me out. Thanks. Susmita
63
by: Nick Palmer | last post by:
Hi all, Is there a DB2 equivilant to Oracle's DB Link functionality ? I have two DB2 databases and I need to get access to the tables in one from the other. In Oracle I would just create a DB...
3
by: Jon Ole Hedne | last post by:
My Access 2002-application need to work with tables from both Oracle and Access. To solve this, I want to run some querys on three views in Oracle and import the results into temporary...
13
by: Chris Botha | last post by:
The machine is running XP Pro with all the latest service packs, etc. I must access an Oracle database so I installed the Oracle client stuff. I can query Oracle from a Windows app, no problem....
2
by: Vinod Sadanandan | last post by:
All, Below listed are the new features in Oracle 11g ,please join me in this discussion to generate a testcase and analyze each of the listed features . Precompilers:...
2
by: Ruslan A Dautkhanov | last post by:
Hello ! I'm about to install O9i on FreeBSD box. uname -a: FreeBSD stat2.scn.ru 5.2.1-RELEASE-p3 FreeBSD 5.2.1-RELEASE-p3 #2: Fri Apr 23 19:19:43 KRAST 2004...
0
by: Jack | last post by:
Training Classes for Oracle10g, 9i, 8i Certification training in Oracle10g and 9i: DBA, Developer, Discoverer. training conducted at your location worldwide. Courseware licensing also available....
0
by: Winder | last post by:
Training Classes for Oracle10g, 9i, 8i Certification training in Oracle10g and 9i: DBA, Developer, Discoverer. training conducted at your location worldwide. Courseware licensing also available....
0
by: sathyguy | last post by:
when i type the below in my RHEL AS 4's Firefox 1.5 http://appsworld.ncc.com:7777/forms/...&form=test.fmx iam getting the below error... The requested URL /forms/frmservlet was not found on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.