SQLite - C/C++
Installation
Before using SQLite in C/C++ programs, we need to make sure the SQLite library is already available on the machine. You can check the SQLite Installation chapter for the installation process.
C/C++ Interface API
The following are important C&C++ / SQLite interface routines that meet your needs for using an SQLite database in C/C++ programs. If you need more details, please refer to the official SQLite documentation.
| No. | API & Description |
|---|---|
| 1 | **sqlite3_open(const char *filename, sqlite3 ppDb) |
|
This routine opens a connection to an SQLite database file and returns a database connection object for use by other SQLite programs. If both the filename If the parameter is NULL or ':memory:', then sqlite3_open() will create an in-memory database in RAM, which lasts only for the duration of the session. If the filename is not NULL, then sqlite3_open() will use this parameter value to try to open the database file. If a file with that name does not exist, sqlite3_open() will create a new database file named accordingly and open it. |
|
| 2 | *sqlite3_exec(sqlite3, const char *sql, sqlite_callback, void *data, char errmsg) |
|
This routine provides a shortcut for executing SQL commands supplied by the sql parameter, which may consist of multiple SQL commands. Here, the first parameter sqlite3 is the opened database object,sqlite_callback is a callback,data with it as its first argument; errmsg will be returned to retrieve any errors generated by the program. The sqlite3_exec() routine parses and executes each command given by the sql parameter, until the end of the string or an error is encountered. |
|
| 3 | sqlite3_close(sqlite3)* |
|
This routine closes a database connection previously opened with sqlite3_open(). All statements associated with the connection should be finalized before the connection is closed. If any queries are still unfinished, sqlite3_close() will return the SQLITE_BUSY error message refusing to close. |
Connect to the database
The following C code snippet shows how to connect to an existing database. If the database does not exist, it will be created, and finally a database object will be returned.
#include
#include
int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("test.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}else{
fprintf(stderr, "Opened database successfully\n");
}
sqlite3_close(db);
}
Now, let's compile and run the above program to create our database in the current directory test.db. You can change the path as needed.
$gcc test.c -l sqlite3
$./a.out
Opened database successfully
If you want to use C++ source code, you can compile the code as shown below:
$g++ test.c -l sqlite3
Here, we link our program with the sqlite3 library to provide the necessary functions to the C program. This will create a database file test.db in your directory, and you will get the following result:
-rwxr-xr-x. 1 root root 7383 May 8 02:06 a.out
-rw-r--r--. 1 root root 323 May 8 02:05 test.c
-rw-r--r--. 1 root root 0 May 8 02:06 test.db
Create a table
The following C code snippet will be used to create a table in the previously created database:
#include
#include
#include
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i
When the above program is compiled and executed, it creates the COMPANY table in the test.db file, and the final file list is shown below:
-rwxr-xr-x. 1 root root 9567 May 8 02:31 a.out
-rw-r--r--. 1 root root 1207 May 8 02:31 test.c
-rw-r--r--. 1 root root 3072 May 8 02:31 test.db
INSERT Operation
The following C code snippet shows how to create records in the COMPANY table created above:
#include
#include
#include
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i
When the above program is compiled and executed, it will create the given records in the COMPANY table and display the following two lines:
Opened database successfully
Records created successfully
SELECT Operation
Before we begin explaining the examples of fetching records, let us first understand some details of the callback function, which will be used in our examples. This callback provides a way to obtain results from a SELECT statement. It is declared as follows:
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 */
);
If the above callback is used as the third argument in the sqlite_exec() program, SQLite will call this callback function for every record processed in each SELECT statement executed within the SQL parameters.
The following C code segment shows how to fetch and display records from the COMPANY table created earlier:
#include
#include
#include
static int callback(void *data, int argc, char **argv, char **azColName){
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i=0; i
When the above program is compiled and executed, it will produce the following result:
Opened database successfully
Callback function called: ID = 1
NAME = Paul
AGE = 32
ADDRESS = California
SALARY = 20000.0
Callback function called: ID = 2
NAME = Allen
AGE = 25
ADDRESS = Texas
SALARY = 15000.0
Callback function called: ID = 3
NAME = Teddy
AGE = 23
ADDRESS = Norway
SALARY = 20000.0
Callback function called: ID = 4
NAME = Mark
AGE = 25
ADDRESS = Rich-Mond
SALARY = 65000.0
Operation done successfully
UPDATE Operation
The following C code snippet shows how to use an UPDATE statement to update any record, and then retrieve and display the updated record from the COMPANY table:
#include
#include
#include
static int callback(void *data, int argc, char **argv, char **azColName){
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i=0; i
When the above program is compiled and executed, it will produce the following result:
Opened database successfully
Callback function called: ID = 1
NAME = Paul
AGE = 32
ADDRESS = California
SALARY = 25000.0
Callback function called: ID = 2
NAME = Allen
AGE = 25
ADDRESS = Texas
SALARY = 15000.0
Callback function called: ID = 3
NAME = Teddy
AGE = 23
ADDRESS = Norway
SALARY = 20000.0
Callback function called: ID = 4
NAME = Mark
AGE = 25
ADDRESS = Rich-Mond
SALARY = 65000.0
Operation done successfully
DELETE Operation
The following C code segment shows how to use the DELETE statement to delete any records, and then retrieve and display the remaining records from the COMPANY table:
#include
#include
#include
static int callback(void *data, int argc, char **argv, char **azColName){
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i=0; i
When the above program is compiled and executed, it will produce the following result:
Opened database successfully
Callback function called: ID = 1
NAME = Paul
AGE = 32
ADDRESS = California
SALARY = 20000.0
Callback function called: ID = 3
NAME = Teddy
AGE = 23
ADDRESS = Norway
SALARY = 20000.0
Callback function called: ID = 4
NAME = Mark
AGE = 25
ADDRESS = Rich-Mond
SALARY = 65000.0
Operation done successfully