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?