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.c c
//-----------------------------------------------------------
// 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.c c,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_cas t<const dvoid *>( HEnv ),
reinterpret_cas t<dvoid **>( &HService ), OCI_HTYPE_SVCCT X, 0, 0 );
OCIHandleAlloc( reinterpret_cas t<const dvoid *>( HEnv ),
reinterpret_cas t<dvoid **>( &HError ), OCI_HTYPE_ERROR , 0, 0 );
OCIHandleAlloc( reinterpret_cas t<const dvoid *>( HEnv ),
reinterpret_cas t<dvoid **>( &HServer ), OCI_HTYPE_SERVE R, 0, 0 );
}
bool COCIConnection: :Connect()
{
try
{
SetStatus( OCIServerAttach ( HServer, HError, Server.size() ?
reinterpret_cas t<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_SVCCT X, HServer, 0,
OCI_ATTR_SERVER , HError ) );
if( GetStatus() != OCI_SUCCESS )
{
cerr << __FILE__ << ":" << __LINE__ << " " << Report( HError ) << endl;
}
else
{
SetStatus( OCIHandleAlloc( reinterpret_cas t<const dvoid *>( HEnv ),
reinterpret_cas t<dvoid **>( &HAuth ), OCI_HTYPE_SESSI ON, 0, 0 ) );
if( GetStatus() != OCI_SUCCESS )
{
cerr << __FILE__ << ":" << __LINE__ << " " << Report( HError ) << endl;
}
else
{
OCIAttrSet( HAuth, OCI_HTYPE_SESSI ON, const_cast<dvoi d *>(
reinterpret_cas t<const dvoid *>( User.c_str() ) ), User.size(),
OCI_ATTR_USERNA ME, HError );
OCIAttrSet( HAuth, OCI_HTYPE_SESSI ON, const_cast<dvoi d *>(
reinterpret_cas t<const dvoid *>( Password.c_str( ) ) ), Password.size() ,
OCI_ATTR_PASSWO RD, 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_SVCCT X, HAuth, 0, OCI_ATTR_SESSIO N,
HError );
}
}
}
}
}
catch( ... ){}
return OCI_SUCCESS == GetStatus();
}
COCIStatement *COCIConnection ::CreateStateme nt( const string& Statement )
{
COCIStatement *OCIStatement = 0;
if( OCI_SUCCESS == GetStatus() )
{
OCIStatement = new COCIStatement( HService, HEnv, HError, Statement );
}
return OCIStatement;
}
COCIConnection: :~COCIConnectio n()
{
if( Session )
{
OCISessionEnd ( HService, HError, HAuth, OCI_DEFAULT );
}
if( Attached )
{
OCIServerDetach ( HServer, HError, OCI_DEFAULT );
}
if( HServer )OCIHandleFree( HServer, OCI_HTYPE_SERVE R );
if( HService )OCIHandleFree( HService, OCI_HTYPE_SVCCT X );
if( HAuth )OCIHandleFree( HAuth, OCI_HTYPE_SESSI ON );
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->CreateStatemen t( "SELECT
TABLESPACE_NAME FROM DBA_FREE_SPACE" );
COCIStatement *Statement = Connection->CreateStatemen t(
"SELECT a.tablespace_na me TSNAME, "
"SUM(a.sumb)*10 0/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_na me "
"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*******@gmai l.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
How to connect to oracle using C++ program and select some data from a
table?