Quantcast
Channel: askyb.com » Linux
Viewing all articles
Browse latest Browse all 5

C++ SQLite Example in Linux

$
0
0
This example will demonstrate how you can write a C++ program to utilize SQLite as your program database file in Linux environment. I am using Ubuntu in this demonstration. If you are new to C++ & SQLite in Ubuntu, it is advisable that you read the following article so that you have better understanding what to do before you can compile your c++ program and utilize SQLite in your system. Compiling C++ code by using g++ in Ubuntu Installing SQLite in Ubuntu To start the demo, let’s create a cpp file now and I rename it as SqliteSample.cpp. Also, include the the following header file from sqlite to our demo program.
#include <sqlite3.h>
We now create a new callback function. The callback function is triggered once there is data ready to display. This usually happen when the select statement being executed.
1
2
3
4
5
6
7
8
9
10
11
12
// This is the callback function to display the select data in the table
static int callback(void *NotUsed, int argc, char **argv, char **szColName)
{
  for(int i = 0; i < argc; i++)
  {
    std::cout << szColName[i] << " = " << argv[i] << std::endl;
  }

  std::cout << "\n";

  return 0;
}
In the main function, right after the database being open successfully and before it’s close, we need to insert the following codes into the main function so that our mission in this demonstration can be accomplished.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
int main()
{
  sqlite3 *db;
  char *szErrMsg = 0;

  // open database
  int rc = sqlite3_open("askyb.db", &db);
 
  if(rc)
  {
    std::cout << "Can't open database\n";
  } else {
    std::cout << "Open database successfully\n";
  }

  // prepare our sql statements
  const char *pSQL[6];
  pSQL[0] = "CREATE TABLE Employee(Firstname varchar(30), Lastname varchar(30), Age smallint)";
  pSQL[1] = "INSERT INTO Employee(Firstname, Lastname, Age) VALUES ('Woody', 'Alan', 45)";
  pSQL[2] = "INSERT INTO Employee(Firstname, Lastname, Age) VALUES ('Micheal', 'Bay', 38)";
  pSQL[3] = "SELECT * FROM Employee";

  // execute sql
  for(int i = 0; i < 4; i++)
  {
    rc = sqlite3_exec(db, pSQL[i], callback, 0, &szErrMsg);
    if(rc != SQLITE_OK)
    {
      std::cout << "SQL Error: " << szErrMsg << std::endl;
      sqlite3_free(szErrMsg);
      break;
    }  
  }

  // close database
  if(db)
  {
    sqlite3_close(db);
  }

  return 0;
}
To compile this program, we will need to link the sqlite3 library along with the program as following:
$ g++ SqliteSample.cpp -l sqlite3 -o SqliteSample
If you compiled and run the C++ program now, you should see a similar screenshot as following. c++ example in ubuntu Open the askyb.db file from Sqlite console will show you the data as following: c++ example in ubuntu Download sample source code: SQLiteSample.cpp

Viewing all articles
Browse latest Browse all 5

Trending Articles