Sqlite C - C++ Tutorial PDF
Sqlite C - C++ Tutorial PDF
Installation
Before we start using SQLite in our C/C++ programs, we need to make sure that we have SQLite
library set up on the machine. You can check SQLite Installation chapter to understand installation
process.
This routine opens a connection to an SQLite database file and returns a database
connection object to be used by other SQLite routines.
If filename is not NULL, sqlite3_open attempts to open the database file by using its value.
If no file by that name exists, sqlite3_open will open a new database file by that name.
This routine provides a quick, easy way to execute SQL commands provided by sql
argument which can consist of more than one SQL command.
Here, first argument sqlite3 is open database object, sqlite_callback is a call back for
which data is the 1st argument and errmsg will be return to capture any error raised by
the routine.
The sqlite3_exec routine parses and executes every command given in the sql argument
until it reaches the end of the string or encounters an error.
3 sqlite3_closesqlite3 ∗
This routine closes a database connection previously opened by a call to sqlite3_open. All
prepared statements associated with the connection should be finalized prior to closing
the connection.
If any queries remain that have not been finalized, sqlite3_close will return SQLITE_BUSY
with the error message Unable to close due to unfinalized statements.
Connecting To Database
Following C code segment shows how to connect to an existing database. If database does not
exist, then it will be created and finally a database object will be returned.
#include <stdio.h>
#include <sqlite3.h>
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 above program to create our database test.db in the current directory.
You can change your path as per your requirement.
If you are going to use C++ source code, then you can compile your code as follows:
Here we are linking our program with sqlite3 library to provide required functions to C program.
This will create a database file test.db in your directory and you will have the result something as
follows:
Create a Table
Following C code segment will be used to create a table in previously created database:
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
/* Open database */
rc = sqlite3_open("test.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}else{
fprintf(stdout, "Opened database successfully\n");
}
/* Create SQL statement */
sql = "CREATE TABLE COMPANY(" \
"ID INT PRIMARY KEY NOT NULL," \
"NAME TEXT NOT NULL," \
"AGE INT NOT NULL," \
"ADDRESS CHAR(50)," \
"SALARY REAL );";
When above program is compiled and executed, it will create COMPANY table in your test.db and
final listing of the file will be as follows:
INSERT Operation
Following C code segment shows how we can create records in our COMPANY table created in
above example:
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
/* Open database */
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");
}
When above program is compiled and executed, it will create given records in COMPANY table and
will display following two line:
SELECT Operation
Before we proceed with actual example to fetch records, let me give a little detail about the
callback function, which we are using in our examples. This callback provides a way to obtain
results from SELECT statements. It has the following declaration:
If above callback is provided in sqlite_exec routine as the third argument, SQLite will call the this
callback function for each record processed in each SELECT statement executed within the SQL
argument.
Following C code segment shows how we can fetch and display records from our COMPANY table
created in above example:
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
static int callback(void *data, int argc, char **argv, char **azColName){
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i=0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
/* Open database */
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");
}
When above program is compiled and executed, it will produce the following result:
UPDATE Operation
Following C code segment shows how we can use UPDATE statement to update any record and
then fetch and display updated records from our COMPANY table:
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
static int callback(void *data, int argc, char **argv, char **azColName){
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i=0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
const char* data = "Callback function called";
/* Open database */
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");
}
When above program is compiled and executed, it will produce the following result:
DELETE Operation
Following C code segment shows how we can use DELETE statement to delete any record and then
fetch and display remaining records from our COMPANY table:
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
static int callback(void *data, int argc, char **argv, char **azColName){
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i=0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
/* Open database */
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");
}
When above program is compiled and executed, it will produce the following result: