Database connectivity using C/C++ Tutorial!!!
I thought of putting this tutorial here in CE, so that it would be useful for other CEans!! Some of you out there would have never heard of this too, "Connecting Database with C++ program???" right, anyways no prob's CE is here to help you out 😉(and enlighten you!!! )
SO here is the Tutorial for making your C++ program to communicate with what ever database you want..... (ie., Oracle, Sybase, MySQL etc....)
The Tutorial is broken down from the very basics,
Before starting the Tutorial I would like to introduce you the following:
-> Compiler: Code::Blocks IDE with MinGW compiler Download Link: #-Link-Snipped-#
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: <a href="https://www.sqlapi.com/Download/index.html" target="_blank" rel="nofollow noopener noreferrer">Download | SQLAPI++</a>
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 (smart way in other terms).
-> OCCI: Oracle C++ Call Interface Download Link: #-Link-Snipped-#
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 :
[B] 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[/B]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)
If you using Linux OS:
[B] /home/anandakumar/Desktop/SQLAPI/lib/libsqlapi.so /home/anandakumar/Desktop/SQLAPI/lib/libsqlapi.a /usr/lib/libcodeblocks.so[/B]And similarly for MAC OS,
For those of you who are wondering why we are including these files (library files), Its to Link the C/C++ Program with the SQLAPI++.
Now we are ready to Go guys......
First simple Program:
This program illustrates you how to connect and Disconnect from Database:
[B]#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( "arvind", // database name "slashfear", // user name "Crazy Engineers", // 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; } [/B]The output of the program is simple as shown below,
OutPut:
We are Connected! We are Disconnected!Examining the above Code:
#include <SQLAPI.h> - main header, should be used whenever SQLAPI++ is used.
SAConnection con - To connect to a database you should create a connection object and then connect it., here con is the connection object
con.Connect( "arvind", "slashfear", "Crazy Engineers", SA_Oracle_Client); - This syntax is used for establishing connection with the database. Here the first value is the name of the database (DNS name), the second value is the user name and the third user name is the password. Then the SA_Oracle_Client is the client name we are going to use (In our example its oracle )
con.Disconnect â This syntax is used for disconnecting from the database.
Simple isn't it ;-)!!! That's It we are now able to successfully connected and disconnected from the DATABASE, Now to go one step further..... That's how to execute a SQL query like creating a table and inserting values in the table. This is covered in the next Post.... stay connected;-)
-Arvind(slashfear)