SQL using C/C++ and SQLite
In this article, we’d like to introduce the article about SQLITE combined with C++ or C.
Before we go on with this tutorial, we need to follow the SQLITE3 installation procedure that can be easily found here. At the same time it is required a basic knowledge of SQL.
We will show the following operations:
- Database Connection/Creation
- Create Table
For the sake of simplicity, let’s use a simple database made of just one table.
Database Connection/Creation of Table
In this snippet, we will use two routines contained in sqlite3.h library.
- sqlite3_open(const char *filename, sqlite3 **ppDb) - sqlite3_close(sqlite3 *ppDb)
The Compiling is executed by the add of command -l sqlite3 .
$ g++ createDB.cpp -l sqlite3 $ ./a.out Opened Database Successfully! $ ls create.cpp a.out example.db
The first routine returns an integer: if the integer is equal to 0, it succeeded. If the database doesn’t exist yet, it is going to be created in the same directory where the process was executed.
The second one just closes the connection previously opened by SQLITE3_OPEN().
All prepared statement associated with the connection should be finalized prior to closing the connection.
In this snippet we will use the routine:
- sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void *data, char **errmsg)
$ g++ createTable.cpp -l sqlite3 $ ./a.out Table created Successfully
This routine returns an integer: if the integer is equal to the SQLITE_OK macro, everything worked well.
For this example, the controls about open of Database are omitted, that are written in the past code.
Insert and Delete
We will use the SQLITE3_EXEC() routine for Insert as well. Procedure and checks are similar to the previous one. There is just one difference in the string that we use with the SQLITE3_EXEC().
$ g++ insertDelete.cpp -l sqlite3 $ ./a.out STATE OF TABLE BEFORE INSERT Records created Successfully! STATE OF TABLE AFTER INSERT ID = 1 NAME = STEVE SURNAME = GATES AGE = 30 ADDRESS = PALO ALTO SALARY = 1000.0 ID = 2 NAME = BILL SURNAME = ALLEN AGE = 20 ADDRESS = SEATTLE SALARY = 300.22 ID = 3 NAME = PAUL SURNAME = JOBS AGE = 24 ADDRESS = SEATTLE SALARY = 9900.0 Record deleted Successfully! STATE OF TABLE AFTER DELETE OF ELEMENT ID = 1 NAME = STEVE SURNAME = GATES AGE = 30 ADDRESS = PALO ALTO SALARY = 1000.0 ID = 3 NAME = PAUL SURNAME = JOBS AGE = 24 ADDRESS = SEATTLE SALARY = 9900.0
Before we go on showing the Select operation, that we might just as easily consider the most important of all the tutorial, let’s have a look at the Callback prototype, that we are going to use in our example.
This operation lets us to get an output from the Select operation:
typedef int (*sqlite3_callback)( void*, /* Data provided in the 4th argument of sqlite3_exec() */ int, /* The number of columns in row */ char**, /* An array of strings representing fields in the row */ char** /* An array of strings representing column names */ );
Now, we will use the Callback function into the Select as shown in the next snippet:
$ g++ select.cpp -l sqlite3
Opened Database Successfully! RESULT OF SELECT ID = 1 NAME = STEVE SURNAME = GATES AGE = 30 ADDRESS = PALO ALTO SALARY = 1000.0 ID = 3 NAME = PAUL SURNAME = JOBS AGE = 24 ADDRESS = SEATTLE SALARY = 9900.0 Operation OK!
This article provides a quick guide to Databases with SQLITE3 using C++/C.
In our examples, we used C++, but when we passed our strings, we called the c_str() method, in order to convert the string in a character array: this proves that all the routines utilised can be utilised in C as well.