How to Get SQLite Working in Windows 8 Apps

  1. First, download and install the SQLite VSIX for Windows RT. I don’t think I found this step in any tutorial:

    https://www.sqlite.org/download.html

  2. Change the project’s build platform to x86 (you’ll find it in the top toolbar, where it should originally say “Any CPU“).

    3) Now go to Add Reference -> Windows -> Extensions -> SQLite for Windows Runtime. Your references will then look like this:


    References after adding SQLiteRT

    This on its own only adds support for SQLite, but not any classes or other code elements.

  3. Open the Nuget Package Manager and search for sqlite-net:


    Searching for sqlite-net in the NuGet manager

  4. sqlite-net doesn’t add any reference to your project, but it creates 2 files for you:


    Files after adding sqlite-net

  5. Write a class that will correspond to 1 row in the table. Don’t forget to add the PrimaryKey attribute to 1 property:
    C#
    class Row
    {
        [PrimaryKey()]
        public int ID
        {
            get; set;
        }
        public string Retezec
        {
            get; set;
        }
    }
  6. Write code that uses this class. My sample code will give you an idea of how to use SQLite in Windows 8 (then use the debugger and a breakpoint to verify that the row you created in the if clause was actually created):
    C#
    string databasePath = Windows.Storage.ApplicationData.Current.LocalFolder.Path + "db.db3";
    SQLiteConnection connection = new SQLiteConnection(databasePath);
    Row row = null;
    bool insert = false;
    if (insert)
    {
        row = new Row();
        row.ID = 1;
        row.Retezec = "Retezec do DB";
        connection.CreateTable<Row>();
        connection.Insert(row);
    }
    else
    {
        row = connection.Get<Row>(1);
    } </Row></Row>

And that’s all there is to it. I hope this will be useful to someone. One thing I forgot to mention at the start: if you want to try this out, you need Visual Studio Express 2012 for Windows 8 or newer.

Categories

Leave a Reply