{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
Compiling SQLite with Visual Studio 2008
Summary
This article shows how to set up a project and compile the SQLite shell and database engine source code with Visual Studio. Screenshots included.
 
Table of Contents

The Source Files

New Project from Visual Studio

Figure 1. New Project for SQLite

Figure 2. Win32 Application Wizard

Adding SQLite files

Figure 3. The compiled sqlite3.exe

Testing the compiled SQLite Source

Breakpoint in the main()

Code Listing 1. Beginnings of main() function

In Memory DB

Code Listing 2. In Memory DB

The Database File

Figure 4. The property pages for specifying the db file

 

The Source Files

In this project, we will be compiling the sqlite3.exe shell program. The database functionality will be included in this executable program. If you want just a dll (sqlite3.dll) for the database functionality, you can exclude the shell.c below and compile the rest as a dll.

Now for the required source files:

  • sqlite3.c
  • sqlite3.h
  • sqlite3ext.h
  • shell.c

The first three files – sqlite3.c, sqlite3.h, and sqlite3ext.h – contain the source for database engine. The file sqlite3.c is the amalgamation of several little .c files that make up database engine. You will get these files when you download the sqlite-amalgamation-3_5_7.zip (or whatever the latest version is) from the sqlite.org site.

The file shell.c contains code for the command line program that can be used to create SQLite databases, execute SQL commands, etc. This needs to be downloaded separately from the source code repository of sqlite.org site:

shell.c from sqlite.org

If the above link changed, go to the cvs on sqlite.org and find shell.c file.

New Project from Visual Studio

From Visual Studio, choose ‘File – New Project’ menu. This opens the New Project dialog box. Since the source for SQLite is in C programming language, choose ‘Visual C++ - Win32 – Win32 Console Application’ as the project type.

Figure 1. New Project for SQLite

Figure 1. New Project for SQLite

As shown in figure 1, the project is named sqlite3 just so it makes sqlite3.exe automatically (without any other configuration changes).

After clicking OK, Visual Studio will display Win32 Application Wizard. In the second screen choose ‘Empty Project’, because we will add all the necessary files.

Figure 2. Win32 Application Wizard

Figure 2. Win32 Application Wizard

Adding SQLite files

After clicking Finish in the above dialog box, you will have the solution files (sqlite3.sln, sqlite3.suo, and sqlite3.ncb) and project files (sqlite3.vcproj, etc) in the appropriate directories.

Now we need to add the sqlite source files. Best way is to copy all the four files - sqlite3.c, sqlite3.h, sqlite3ext.h, shell.c – into the project directory we set up above.

The Solution / Project in Visual Studio will have three empty directories: Header Files, Resource Files, and Source Files.

  • Right-Click on the Source Files directory in Visual Studio Solution and choose ‘Add – Existing Item’ and then add shell.c and sqlite3.c files.
  • Then Right-Click on the Header Files directory in Visual Studio Solution and choose ‘Add – Existing Item’ and add the header files sqlite3.h and sqlite3ext.h.

(All files could have been added into the same Source Files directory as well)

Now press F5 to compile and Run. You should see the familiar sqlite shell.

Figure 3. The compiled sqlite3.exe

Figure 3. The compiled sqlite3.exe

By default, of course, this executable will be compiled into the Debug directory. Should you choose, you can make a Release build by choosing that option in Visual Studio. But the Debug option lets you step through the code, etc.

After compilation you might also get lots of warnings (some 122!). There was some discussion on sqlite.org, but the bottom line is just ignore those warnings.

Now if you look in the debug directory (where you compiled this code to), you will see the executable sqlite3.exe but not sqlite3.dll. All the database engine functionality is nicely compiled into that executable (size of the release version is 367KB and the debug version is 610KB with the default Visual Studio settings).

Testing the compiled SQLite Source

Breakpoint in the main()

To get familiarized with the code, you can put a breakpoint at the beginning of the main() function in shell.c.

Code Listing 1. Beginnings of main() function

int main(int argc, char **argv){
  char *zErrMsg = 0;
  struct callback_data data;
  const char *zInitFile = 0;
  char *zFirstCmd = 0;
  int i;
  int rc = 0;

  Argv0 = argv[0];
  main_init(&data);
  stdin_is_interactive = isatty(0);

  /* Make sure we have a valid signal handler early, before anything
  ** else is done.
  */
#ifdef SIGINT
  signal(SIGINT, interrupt_handler);
#endif

  /* Do an initial pass through the command-line argument to locate
  ** the name of the database file, the name of the initialization file,
  ** and the first command to execute.
  */
  for(i=1; i<argc-1; i++){



In Memory DB

If you do not specify a filename for the database, the database will be created in the memory. In essence the following line gets executed (and once you are done playing with sqlite3.exe the db will be gone, if you created one)

Code Listing 2. In Memory DB

#ifndef SQLITE_OMIT_MEMORYDB
    data.zDbFilename = ":memory:";
#else
    data.zDbFilename = 0;
#endif

The Database File

You can also specify a database file as a command argument using the Property Pages for the project. Here test.db is used as the db file; the created database will be in that file; it will still be there after you exit sqlite3.exe program.

Figure 4. The property pages for specifying the db file

Figure 4. The property pages for specifying the db file
Bookmark and Share This

More Articles With Similar Tags
This article talks about various aspects of creating an SQLite database. It also uses Android SQLite shell as an example.
This article explains how to get pretty / nice output from the SQLite command line shell program.
icon-android-shell-main-method.jpg
This article talks about the SQLite packages available in Android SDK.
icon-android-sqlite-query.jpg
This article looks at using SQLite from the Android shell.
icon-sqlite-db-browser-data.jpg
This article discusses creating SQLite databases using GUI-based database managers. The example used here is SQLite Database Browser.
About  Contact  Privacy Policy  Site Map