Question :How to connect c/c++ with database by using its available header files?
Question From :
Deepu
3rd B.Sc Computer Science
Farook college ,Calicut
E-mail: deepup226@gmail.com
Before we start just to introduce some SQL commands and stuff. For starter you had some back grounds on it….
SQL (Structured Query Language) is a fourth-generation language (4GL) that is used to define, manipulate, and control an RDBMS (relational database management system).
DML sublanguage is concerned with commands that can perform operations on data in tables.It provides four commands for data manipulation:
SELECT command retrieves data for queries.
INSERT command adds new rows into a database table.
UPDATE command changes the values of data items in a database table.
DELETE command removes rows from a database table.
*) Compiler: Code::Blocks IDE with MinGW compiler Download Link: Download binary
The reason behind choosing this compiler is simple, Code::Blocks is a cross compiler (It can run on any platform like windows, Linux and Mac and hence called as a cross compiler) And it is Free to download. This IDE is specially designed for C and C++ and easy to use.
*)API: We are going to use SQLAPI++ Download Link: SQLAPI++ Library - Download
SQLAPI++ is a API ( Application Programming Interface) designed specially for C++ database communication(Nothing but a set of header files), This is the Key for communicating with the database, you have other methods also like Pro*C (comes along with your ORACLE) and other methods too, but I choose this SQLAPI++ because it is easy and makes your work simple .
*)OCCI: Oracle C++ Call Interface Download Link: OCCI Downloads
OCCI is an interface defined by the database company ORACLE that defines for the C++ programmer a comfortable interface to access the ORACLE database with classes using parameters that are reminiscent of SQL statements. The interface exists for ORACLE 9i and ORACLE 10g. OCCI comes along with your ORACLE but still If you guys don't have it, you can download it from the above link (NOTE: You have to be a member to download that file, Its simple just fill in a forum and you are a member!!!)
Download and install the above three (If you dont have), Now we are almost ready to start,
Prior Thing to be done before starting:
-> Open the code::blocks IDE and go to or click on settings -> compiler and debugger settings (You will now see global compiler settings)
-> Now click on “Linker settings” in the linker settings click on ADD button and add the following
If your using windows OS :
Question From :
Deepu
3rd B.Sc Computer Science
Farook college ,Calicut
E-mail: deepup226@gmail.com
Before we start just to introduce some SQL commands and stuff. For starter you had some back grounds on it….
SQL (Structured Query Language) is a fourth-generation language (4GL) that is used to define, manipulate, and control an RDBMS (relational database management system).
DML sublanguage is concerned with commands that can perform operations on data in tables.It provides four commands for data manipulation:
SELECT command retrieves data for queries.
INSERT command adds new rows into a database table.
UPDATE command changes the values of data items in a database table.
DELETE command removes rows from a database table.
Before starting the Tutorial I would like to introduce you the following:
The reason behind choosing this compiler is simple, Code::Blocks is a cross compiler (It can run on any platform like windows, Linux and Mac and hence called as a cross compiler) And it is Free to download. This IDE is specially designed for C and C++ and easy to use.
*)API: We are going to use SQLAPI++ Download Link: SQLAPI++ Library - Download
SQLAPI++ is a API ( Application Programming Interface) designed specially for C++ database communication(Nothing but a set of header files), This is the Key for communicating with the database, you have other methods also like Pro*C (comes along with your ORACLE) and other methods too, but I choose this SQLAPI++ because it is easy and makes your work simple .
*)OCCI: Oracle C++ Call Interface Download Link: OCCI Downloads
OCCI is an interface defined by the database company ORACLE that defines for the C++ programmer a comfortable interface to access the ORACLE database with classes using parameters that are reminiscent of SQL statements. The interface exists for ORACLE 9i and ORACLE 10g. OCCI comes along with your ORACLE but still If you guys don't have it, you can download it from the above link (NOTE: You have to be a member to download that file, Its simple just fill in a forum and you are a member!!!)
Download and install the above three (If you dont have), Now we are almost ready to start,
Prior Thing to be done before starting:
-> Open the code::blocks IDE and go to or click on settings -> compiler and debugger settings (You will now see global compiler settings)
-> Now click on “Linker settings” in the linker settings click on ADD button and add the following
If your using windows OS :
Code:
C:\SQLAPI\lib\libsqlapiddll.a C:\Program Files\CodeBlocks\MinGW\lib\libuser32.a C:\Program Files\CodeBlocks\MinGW\lib\libversion.a C:\Program Files\CodeBlocks\MinGW\lib\liboleaut32.a C:\Program Files\CodeBlocks\MinGW\lib\libole32.a
These will be found in your SQLAPI++ (If you have not extracted in C: drive then select the appropriate location and add the mentioned files to linker settings) First simple Program: This program illustrates you how to connect and Disconnect from Database:
Code:
#include <stdio.h> // for printf
#include <SQLAPI.h> // main SQLAPI++ header
int main(int argc, char* argv[])
{
SAConnection con; // create connection object
try
{
// connect to database
// in this example it is Oracle,
// but can also be Sybase, Informix, DB2
// SQLServer, InterBase, SQLBase and ODBC
con.Connect(
"Anoop", // database name
"mydb", // user name
"mydbpw", // password
SA_Oracle_Client); //client name
printf("We are connected!\n");
// Disconnect is optional
// autodisconnect will ocur in destructor if needed
con.Disconnect();
printf("We are disconnected!\n");
}
catch(SAException &x)
{
// SAConnection::Rollback()
// can also throw an exception
// (if a network error for example),
// we will be ready
try
{
// on error rollback changes
con.Rollback();
}
catch(SAException &)
{
}
// print error message
printf("%s\n", (const char*)x.ErrText());
}
return 0;
}
OutPut:
OutPut:
We are Connected! We are Disconnected!
0 comments:
Post a Comment