By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

oracle-c++

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

Dec 10 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a

Bob Hairgrove wrote:
You have posted three duplicate


Hmm, does that mean six?

Triplicate :-)

Gavin Deane

Dec 10 '05 #3

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.