Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to communicate Unix / C++ with Oracle?

Status
Not open for further replies.

CMyLove

Programmer
Jan 27, 2001
6
US
Hi Guys
Can somebody tell me how to establish a connection between C++ on Unix and Oracle Database which is on the same machine, please ? Please let me know a name of a good book on the same subject. Thanks.
 
Hmm.. you probably want to look into odbc, and there are any number of books, but I don't know a good one. I use Informix, and they have an embedded sql pre-processor, I think Oracle's is called c* or something like that. This is very easy to use, and I'd try that first of all. As for a good book, I don't know but this thread has been here for awhile so I thought I'd put my 2 cents in.

Good Luck!
mWB. As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
The easiest way is the use Oaracle's ProC. I have included a file that will attach to an oracle database using ProC.

/*----------------------------------------------------------------*/
// Name: TestProC.pc
// Date: 7/24/01
//
// Description: This application is a simple Oracle SQL shell that
// can be used to test various SQL statements.
//
/*----------------------------------------------------------------*/

#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

/*----------------------------------------------------------------*/
// This section is used to define variables that will be used by
// Oracle
/*----------------------------------------------------------------*/

EXEC SQL begin declare section;

varchar user[20];
varchar pass[20];

long oats_roe_id;

EXEC SQL end declare section;


#include <sqlca.h>

/*----------------------------------------------------------------*/
// Procedure delarations
/*----------------------------------------------------------------*/

void sql_error (char *msg);

int main() {

long t1;
long t2;
/*----------------------------------------------------------------*/
// Get user's Oracle username and password and open a session
/*----------------------------------------------------------------*/

printf(&quot;Enter your USERID: &quot;);
scanf(&quot;%s&quot;,user.arr);
user.len = strlen((const char *)user.arr);

system(&quot;stty -echo&quot;);
printf(&quot;Enter your PASSWORD: &quot;);
scanf(&quot;%s&quot;,pass.arr);
pass.len = strlen((const char *)pass.arr);
system(&quot;stty echo&quot;);
printf(&quot;\n&quot;);

/*----------------------------------------------------------------*/
// execute sql_error function when Oracle returns an error
/*----------------------------------------------------------------*/

EXEC SQL whenever sqlerror do sql_error(&quot;Oracle error:&quot;);

/*----------------------------------------------------------------*/
// Connect to database
/*----------------------------------------------------------------*/

EXEC SQL connect :user identified by :pass;

cout << &quot;\nConnected to ORACLE as user: &quot;
<< (char *)user.arr << endl << endl;


/*----------------------------------------------------------------*/
// Build Oracle SQL statement here. Be sure to precede the statement
// with EXEC SQL. Be sure to check for errors before proceeding
// (sqlca.sqlcode...)
/*----------------------------------------------------------------*/

t1 = clock();
EXEC SQL select new_order.oats_roe_id
into :eek:ats_roe_id
from new_order
where new_order.oats_roe_id = 157680;

t2 = clock();
if (sqlca.sqlcode == 0)
cout << &quot;oats_roe_id returned: &quot; << oats_roe_id << endl;
else
cout << &quot;Error: &quot; << sqlca.sqlerrm.sqlerrmc << endl;

cout << &quot;Time to process SQL statement: &quot; << t2-t1 << &quot; sec&quot; << endl;

/*----------------------------------------------------------------*/
// Close connection
/*----------------------------------------------------------------*/

EXEC SQL commit work release;
exit(0);
}

/*----------------------------------------------------------------*/
// Display Oracle error
/*----------------------------------------------------------------*/

void sql_error(char *msg) {
cout << endl << msg << endl;
sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml] = '\0';
cout << sqlca.sqlerrm.sqlerrmc << endl;
exec sql whenever sqlerror continue;
exec sql rollback release;
exit(1);
}

 
Maybe you'll find useful the possibility of calling C functions from Oracle pl/sql too. Search for &quot;external procedures&quot; into Oracle web help if you're interested on it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top