The following steps will guide you through how you can connect to SQLite databases using C++.

1. Download and extract the following files into a single folder C:\SQLite. SQLite Source Code: sqlite-amalgamation-3071100.zip SQLite DLL for Windows: sqlite-dll-win32-x86-3071100.zip SQLite Shell for Windows: sqlite-shell-win32-x86-3071100.zip

2. Let’s create a SQLite database named “test.db” with a single table named “table1”. You might do this at a shell or DOS prompt: Once you’ve done so, the test.db file will be created.

3. You will need to generate the sqlite3.lib file in this example program. Run the command windows again with following commands. Note: If you are using VS2005, please make sure that you copy the file named “mspdb80.dll” to the SQLite DLL folder. Otherwise, the LIB command will fail.

4. Create a new VC++ console application. Let’s called it “sqlite” project.

5. Make sure that you add the “C:\SQLite” file path into the project properties settings as following: [C/C++ -> General -> Additional Includes Directories] [Linker -> General -> Additional Library Directories]

6. Add “sqlite3.lib” to the following property setting: [Linker -> Input -> Additional Dependencies]

7. Let’s enter the following code into the sqlite project main function.

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
// Make sure the you include the sqlite3 header file
#include
#include "sqlite3.h"

int _tmain(int argc, char * argv[])
{
  sqlite3 *db; // sqlite3 db struct
  char *zErrMsg = 0;
  int rc;

  // Open the test.db file
  rc = sqlite3_open(argv[1], &db);

  if( rc ){
  // failed
  fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
  }
  else
  {
  // success
  fprintf(stderr, "Open database successfully\n");
  }

  // Open the test.db file
  sqlite3_close(db);

  getchar();

  return 0;
}

8. Compile the source code and it will generate an executable file. In this tutorial, an EXE named “sqlite.exe” will be generated.

9. Copy the following files to a new folder C:\SQL_Test. Files

10. Go to shell or DOS prompt to test the application: Test