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.
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.
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.
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:
We are Connected! We are Disconnected!
- Executing a simple SQL Command Now, we will look out to execute a simple SQL query.Firstly, creating a table for the database: create table tb1(id number, name varchar(20);
Now, establish the connection to the database then, after your con.connect; method you should use cmd.setCommandText method to pass the query to the database, it is as shown below:
con.Connect("test", "tester", "tester", SA_Oracle_Client); cmd.setCommandText("create table tb1(id number, name varchar(20));”);
- and now, to execute the query we have to use the following command: cmd.Execute(); Full Code:
As we know, Oracle is not auto committed (committing is making permanent reflection of data in the database) so, we have to commit it.
and similarly we can roll back the transactions when an exception occurs, so to do that we use:
For deleting a row, we use this command.
cmd.setCommandText("delete from tb1 where id= 2");
Thus, by the end of this article, we have learned how to connect your C/C++ program to database and perform manipulations.
This article is contributed by Vinay Garg. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above