If your app needs to save user settings or application data between runs SQLite can help you. Even if you want to simply deploy a well-structured app-level read-only configuration along with your binaries, the power and ease of SQLite is hard to beat.
Bringing SQLite into your App
You’d need to download the SQLite source code from here. The download contains 4 files, but you only need 3.
- shell.c – used for compiling a binary to interact with SQLite. (you do not need this one)
- sqlite3.c – source code for SQLite. Yes 1 huge c file.
- sqlite3.h – header file
- sqlite3ext.h – 2nd header file
You only need the last 3 files in your project.
To make sure your sqlite3.c compiles in your WinRT project you need to make the following changes to the properties of this file in your Visual Studio solution. Don’t forget to do this for All configurations and for All platforms, otherwise you will encounter errors when switching between Debug and Release or when switching to target your Surface.
- Disable WinRT Flag. Select properties of the sqlite3.c file. Go to Configuration Properties -> General and switch Consume Windows Runtime Extensions to No.
- Set a compilation flag. Go to the Preprocessor section and insert a SQLITE_OS_WINRT directive.
- Disable Precompiled Headers: Go to the Precompiled Headers section and select Not Using Precompiled Headers in the Precompiled Header value.
Now try compiling your project. It should work.
To use SQLite code from your C++ code base you can simply start calling exported functions. To learn about their API, look at the docs. This can help you get started: http://www.sqlite.org/cintro.html. The docs suggest that you add a special handling for the temp directory from within your WinRT Code. Refer to this doc for more info: http://www.sqlite.org/c3ref/temp_directory.html.
You can also wrap SQLite functions in a proper WinRT wrapper which would be accessible to C# code. You can download wrappers from here. Just be careful, the developer left this line of code there, which I think should be changed:
int ColumnAsDoubleAt(int index)
return sqlite3_column_double(stmt, index);
IMHO, The function should return a double. Even the compiler thinks so. It gives you a warning.
In the next post, I will discuss various uses of SQLite in my App. You can get a sneak peak here.