Friday, 21 July 2017

Database Connectivity using C/C++

SQL (Structured Query Language) is a fourth-generation language (4GL) that is used to define, manipulate, and control an RDBMS (relational database management system).
Before starting the main article, let us get familiar with the used tools.
  1. Compiler: Code::Blocks IDE with MinGW compiler
    Download Link: Binary Download
    Code::Blocks is a cross compiler (It can run on any platform like Windows, Linux and Mac) and it is free to download. This IDE is specially designed for C and C++ and easy to use.

  2. API: We are going to use SQLAPI++ Library
    Download Link: SQLAPI Download
    SQLAPI++ is a C++ library (basically a set of header files) for accessing multiple SQL databases (Oracle, SQL Server, DB2, Sybase, Informix, InterBase, SQLBase, MySQL, PostgreSQL, SQLite, SQL Anywhere and ODBC). It is easy to implement and simple.

  3. OCCI: Oracle C++ Call Interface
    Download Link: OCCI C++ Download 
    OCCI is an interface defined by the database company ORACLE that defines a comfortable interfacefor the C++ programmer to access the Oracle database with classes using parameters that are reminiscent of SQL statements. The interface exists for ORACLE 9i, ORACLE 10 and comes with the Oracle.
We must download and install the above three (if we don’t have them). Now we are almost ready to start.

Some settings 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
For 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).
The above code is used to add library files to connect C/C++ program with SQLAPI.
Basically, there are 2 steps:


Connecting to database (and error handling)      
 
Code:
// C++ pgroram for connecting to database (and error handling)
#include<stdio.h>
#include<SQLAPI.h>         // main SQLAPI++ header
 
int main(int argc, char* argv[])
{
    // create connection object to connect to database
    SAConnection con;
    try
    {
        // connect to database
        // in this example, it is Oracle,
        // but can also be Sybase, Informix, DB2
        // SQLServer, InterBase, SQLBase and ODBC
        con.Connect ("test",    // database name
                     "tester"// user name
                     "tester"// password
                     SA_Oracle_Client); //Oracle Client
        printf("We are connected!\n");
 
        // Disconnect is optional
        // autodisconnect will occur in destructor if needed
        con.Disconnect();
        printf("We are disconnected!\n");
    }
 
    catch(SAException &amp; 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 &amp;)
        {
        }
        // print error message
        printf("%s\n", (const char*)x.ErrText());
    }
    return 0;
}
 
Output:-
            
We are connected!
We are disconnected!

0 comments:

Post a Comment