Home » Server Options » Text & interMedia » connect 2 databases(fetch from one, insert into other) - ProC++ sample code
connect 2 databases(fetch from one, insert into other) - ProC++ sample code [message #75936] Mon, 25 March 2002 10:18 Go to next message
Aparna
Messages: 9
Registered: April 2001
Junior Member
//--------------------------C++ File: Test.cc - entry for main--------------------------
//include c++ std headers, define prototypes in Process.pc ...here...
int main()
{
string usr1 = "user"; //..... define your usr..pwds.. here
connect_database(usr1, pwd1, dbString1, dbName1);
connect_database(usr2, pwd2, dbString2, dbName2);
do_processing(dbName1,dbName2);
}
//--------------------------ProC++ File: Process.pc - Instantiates a ProC++ class----------------------
//#include stdio.h,stdlib.h,sqlca.h ...here
EXEC SQL INCLUDE ProcppClass.H;
int connect_database(string usr, string pwd, string dbString, string dbName)
{
EXEC SQL BEGIN DECLARE SECTION;
char *v_usr = (char *)usr.c_str(); //.. declare for pwd,dbstring & dbName also EXEC SQL END DECLARE SECTION;
EXEC SQL CONNECT :v_usr IDENTIFIED BY :v_pwd AT :v_dbName USING :v_dbString;
if(sqlca.sqlcode)
return -1;
return 1;
}
void disconnect_database(string dbName)
{
EXEC SQL BEGIN DECLARE SECTION;
char *v_dbName = (char *)dbName.c_str();
EXEC SQL END DECLARE SECTION;
EXEC SQL AT :v_dbName COMMIT WORK RELEASE;
}
int do_processing(string dbName1,string dbName2)
{
ProcppClass *ptrTest = new ProcppClass();
try
{
ptrTest->GetRecords(dbName1);
}
catch(int err)
{
cout<<err<<endl;
return -1;
}
while (1)
{
try
{
ptrTest->fetch();
ptrTest->Insertdb2(dbName2);
}
catch (int code)
{
if(code == 1403)
break;
else
cout<<code<<endl;
continue;
}
}
ptrTest->close();
delete ptrTest;
return 1;
}
//--------------------------ProC++ class: ProcppClass.H--------------------------
//#include oraca.h,sqlca.h,sqlca.h,sqlda.h,sqlcpr.h
class ProcppClass
{
public:
ProcppClass();
~ProcppClass();
void GetRecords(string dbName) throw (int);
void fetch() throw (int);
int Insertdb2(string dbName) throw (int);
void close();
void handle_error();
//global
EXEC SQL BEGIN DECLARE SECTION;
int col1;
char col2[[15]];
char col3[[4]];
EXEC SQL END DECLARE SECTION;
private:
//also global
EXEC SQL BEGIN DECLARE SECTION;
SQL_CURSOR rec_cur;
EXEC SQL END DECLARE SECTION;
};
//--------------------------Implement ProC++ class: ProcppClass.pc--------------------------
#define SQLCA_STORAGE_CLASS extern
EXEC SQL INCLUDE ProcppClass.H;
ProcppClass::ProcppClass()
{
}
ProcppClass::~ProcppClass()
{
EXEC SQL FREE :rec_cur;
}
void ProcppClass::GetRecords(string dbName) throw (int)
{
EXEC SQL BEGIN DECLARE SECTION;
char *v_dbName = (char *)dbName.c_str();
EXEC SQL END DECLARE SECTION;

EXEC SQL AT :v_dbName EXECUTE
BEGIN
some_pkg.some_procedure(:rec_cur); //or select statement
END;
END-EXEC;
if(sqlca.sqlcode)
{
handle_error();
throw sqlca.sqlcode;
}
}
void ProcppClass::fetch() throw (int)
{
EXEC SQL FETCH :rec_cur INTO :col1,:col2,:col3; //make sure - datatypes & sizes
if (sqlca.sqlcode == 1403)
throw sqlca.sqlcode; // Like a WHENEVER NOT FOUND statement.
}
void ProcppClass::Insertdb2(string dbName)throw (int)
{
EXEC SQL BEGIN DECLARE SECTION;
char *v_dbName = (char *)dbName.c_str();
EXEC SQL END DECLARE SECTION;

EXEC SQL AT :v_dbName EXECUTE
BEGIN
INSERT INTO some_table (COL1,COL2)VALUES (:col1,:col2);
END;
END-EXEC;
if(sqlca.sqlcode)
{
handle_error();
throw sqlca.sqlcode;
}
}
void ProcppClass::handle_error()
{
//sometimes only part of the message is printed with the following
cout<<sqlca.sqlerrm.sqlerrmc<<endl;
//To get full message text
char sql_error_msg[[256]];
unsigned int buf_len, msg_len;
buf_len = sizeof(sql_error_msg);
sqlglm(sql_error_msg, &buf_len, &msg_len);
cout<<sql_error_msg<<endl;
}
//--------------------------I guess this is the important part--------------------------
//pre-compile
proc UNSAFE_NULL=YES MODE=ORACLE DBMS=V8 CODE=CPP SQLCHECK=FULL LINES=YES CPP_SUFFIX=cc ProcppClass.pc
proc UNSAFE_NULL=YES MODE=ORACLE DBMS=V8 CODE=CPP SQLCHECK=FULL USER=sgs/newuser LINES=YES Process.pc
//compile
CC -I$(ORACLE_HOME)/precomp/public -I$(ORACLE_HOME)/rdbms/public -I$(ORACLE_HOME)/rdbms/demo -I$(ORACLE_HOME)/plsql/public -c ProcppClass.cc
CC -I$(ORACLE_HOME)/precomp/public -I$(ORACLE_HOME)/rdbms/public -I$(ORACLE_HOME)/rdbms/demo -I$(ORACLE_HOME)/plsql/public -c Process.cc
CC -c Test.cc
//link
CC -o Test ProcppClass.o Process.o Test.o -L$(ORACLE_HOME)/lib/ -lclntsh -R$(ORACLE_HOME)/lib -laio -lm -lthread

Thanks
Aparna
Re: connect 2 databases(fetch from one, insert into other) - ProC++ sample code [message #76003 is a reply to message #75936] Tue, 03 December 2002 04:13 Go to previous messageGo to next message
jugal kishor
Messages: 1
Registered: December 2002
Junior Member
i want to see code for c database connection and download proc header file.
thank s u jugal kishor
Re: connect 2 databases(fetch from one, insert into other) - ProC++ sample code [message #76013 is a reply to message #75936] Mon, 06 January 2003 06:28 Go to previous messageGo to next message
gangiredy
Messages: 1
Registered: January 2003
Junior Member
Sir, I want to connect sql database to my c program, how can I do it, I got some sample coding which need sqlca.h header file, It is one of the parts of my final year B-Tech project,expecting your help Thankyou gangs........
Re: connect 2 databases(fetch from one, insert into other) - ProC++ sample code [message #76017 is a reply to message #75936] Mon, 20 January 2003 10:29 Go to previous messageGo to next message
sathigangireddy
Messages: 2
Registered: January 2003
Junior Member
Madam Aparna, Iam gangireddy received ur mail and executed the program and found the following errors:--------------------Configuration: SelMulti - Win32 Debug--------------------
Compiling...
SelMulti.c
D:ProgramsSelMulti.c(109) : error C2059: syntax error : 'string'
D:ProgramsSelMulti.c(123) : error C2059: syntax error : 'string'
D:ProgramsSelMulti.pc(22) : warning C4013: 'sqlcxt' undefined; assuming extern returning int
Error executing cl.exe.

SelMulti.exe - 2 error(s), 1 warning(s)

extern "C" { void sqliem(char *, int *); }

please provide me the solution thanking you gangireddy
Re: connect 2 databases(fetch from one, insert into other) - ProC++ sample code [message #76022 is a reply to message #75936] Mon, 27 January 2003 23:32 Go to previous messageGo to next message
sathigangireddy
Messages: 2
Registered: January 2003
Junior Member
To aparna:

This is a program for connecting to database and insert and fetch values form emp database

/*
* The sqlvcp.pc program demonstrates how you can use the
* sqlvcp() function to determine the actual size of a
* VARCHAR struct. The size is then used as an offset to
* increment a pointer that steps through an array of
* VARCHARs.
*
* This program also demonstrates the use of the sqlgls()
* function, to get the text of the last SQL statement executed.
* sqlgls() is described in the "Error Handling" chapter of
* _The Programmer's Guide to the Oracle Pro*C/C++ Precompiler.
*/

#include <stdio.h>
#include <sqlca.h>
#include <sqlcpr.h>
#include<conio.h>
/* Fake a VARCHAR pointer type. */

struct my_vc_ptr
{
unsigned short len;
unsigned char arr[[32767]];
};

struct
{
char emp_name[[3]][[10]];
int emp_number[[3]];
int dept_number[[3]];
} emp_rec;

/* Define a type for the VARCHAR pointer */
typedef struct my_vc_ptr my_vc_ptr;
my_vc_ptr *vc_ptr;

EXEC SQL BEGIN DECLARE SECTION;
VARCHAR *names;
int limit; /* for use in FETCH FOR clause */
char *username = "scott/tiger@gavaskar";
EXEC SQL END DECLARE SECTION;
void sql_error();
extern void sqlvcp(), sqlgls();

main()
{
unsigned int vcplen, function_code, padlen, buflen;
int i;
char stmt_buf[[120]];

EXEC SQL WHENEVER SQLERROR DO sql_error();

EXEC SQL CONNECT :username;
printf("nConnected.n");

strcpy(emp_rec.emp_name[[0]], "ANQUETIL");
strcpy(emp_rec.emp_name[[1]], "MERCKX");
strcpy(emp_rec.emp_name[[2]], "HINAULT");
emp_rec.emp_number[[0]] = 1964; emp_rec.dept_number[[0]] = 5;
emp_rec.emp_number[[1]] = 1974; emp_rec.dept_number[[1]] = 5;
emp_rec.emp_number[[2]] = 1985; emp_rec.dept_number[[2]] = 5;

EXEC SQL INSERT INTO emp (ename, empno, deptno)
VALUES (:emp_rec);


/* Find number of rows in table. */
EXEC SQL SELECT COUNT(*) INTO :limit FROM emp;

getch();
/* Declare a cursor for the FETCH statement. */
EXEC SQL DECLARE emp_name_cursor CURSOR FOR
SELECT ename FROM emp;
EXEC SQL FOR :limit OPEN emp_name_cursor;

/* Set the desired DATA length for the VARCHAR. */
vcplen = 10;

/* Use SQLVCP to help find the length to malloc. */
sqlvcp(&vcplen, &padlen);
printf("Actual array length of VARCHAR is %ldn", padlen);
getch();
/* Allocate the names buffer for names.
Set the limit variable for the FOR clause. */
names = (VARCHAR *) malloc((sizeof (short) +
(int) padlen) * limit);
if (names == 0)
{
printf("Memory allocation error.n");
getch();
exit(1);
}
/* Set the maximum lengths before the FETCH.
* Note the "trick" to get an effective VARCHAR *.
*/
for (vc_ptr = (my_vc_ptr *) names, i = 0; i < limit; i++)
{
vc_ptr->len = (short) padlen;
vc_ptr = (my_vc_ptr *)((char *) vc_ptr +
padlen + sizeof (short));
}
/* Execute the FETCH. */
EXEC SQL FOR :limit FETCH emp_name_cursor INTO :names;

/* Print the results. */
printf("Employee names--n");
getch();
for (vc_ptr = (my_vc_ptr *) names, i = 0; i < limit; i++)
{
printf
("%.*st(%d)n", vc_ptr->len, vc_ptr->arr, vc_ptr->len);
vc_ptr = (my_vc_ptr *)((char *) vc_ptr +
padlen + sizeof (short));
getch();
}

/* Get statistics about the most recent
* SQL statement using SQLGLS. Note that
* the most recent statement in this example
* is not a FETCH, but rather "SELECT ENAME FROM EMP"
* (the cursor).
*/
buflen = (long) sizeof (stmt_buf);

/* The returned value should be 1, indicating no error. */
sqlgls(stmt_buf, &buflen, &function_code);
if (buflen != 0)
{
/* Print out the SQL statement. */
printf("The SQL statement was--n%.*sn", buflen, stmt_buf);
getch();
/* Print the returned length. */
printf("The statement length is %ldn", buflen);
getch();
/* Print the attributes. */
printf("The function code is %ldn", function_code);
getch();
EXEC SQL COMMIT RELEASE;
exit(0);
}
else
{
printf("The SQLGLS function returned an error.n");
getch();
EXEC SQL ROLLBACK RELEASE;
exit(1);
}
getch();
}

void
sql_error()
{
char err_msg[[512]];
int buf_len, msg_len;


EXEC SQL WHENEVER SQLERROR CONTINUE;

buf_len = sizeof (err_msg);
sqlglm(err_msg, &buf_len, &msg_len);
printf("%.*sn", msg_len, err_msg);
getch();
EXEC SQL ROLLBACK RELEASE;
exit(1);
}

Errors occured in the above program

Pro*C/C++: Release 8.1.6.0.0 - Production on Tue Jan 28 11:27:58 2003

(c) Copyright 1999 Oracle Corporation. All rights reserved.

Error at line 69, column 14 in file C:WINDOWSDesktoponeteo.pc
69 VALUES (:emp_rec);
69 .............1
69 PCC-S-02322, found undefined identifier

please clarify what should i do for getting rid of the above error........Thank u
Re: connect 2 databases(fetch from one, insert into other) - ProC++ sample code [message #76024 is a reply to message #75936] Mon, 10 February 2003 02:49 Go to previous messageGo to next message
Nagendra Singh
Messages: 5
Registered: February 2003
Junior Member
i don't have Pro *c compiler please inform me from where i can free download it.
Connectivity of C+ with SQL [message #76102 is a reply to message #75936] Fri, 16 July 2004 04:59 Go to previous message
Manish Agnihotri
Messages: 1
Registered: July 2004
Junior Member
Please let me know what are the basic requirement
for connectivity of C++ with SQL. What is Pro C++
and how can I obtain it paid/free. Also please let me know tha basic code that may be used for this connectivity. Can TurboC use this feature for connection

Regards

Manish
Previous Topic: Oracle Context - Solution
Next Topic: InterMedia Text on RH Linux 2.1 (Itanium)
Goto Forum:
  


Current Time: Tue Sep 16 22:53:59 CDT 2014

Total time taken to generate the page: 0.30211 seconds