Using SQLite for your Windows RT Apps

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.

sqlite-files

  • 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.

Compiling

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.

  1. Disable WinRT Flag.  Select properties of the sqlite3.c file.  Go to Configuration Properties -> General and switch Consume Windows Runtime Extensions to No.
    sqlite-settings1
  2. Set a compilation flag. Go to the Preprocessor section and insert a SQLITE_OS_WINRT directive.
    sqlite-settings2
  3. Disable Precompiled Headers: Go to the Precompiled Headers section and select Not Using Precompiled Headers in the Precompiled Header value.
    sqlite-settings3

Now try compiling your project. It should work.

Using

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s