Donnerstag, 20. Dezember 2012

Windows Phone 8 SQLite cross platform sample

In addition to my last post about how to use SQLite on Windows Phone 8, I made today a small sample how to use a data access layer for SQLite across several platforms (Windows Phone 8 and Windows 8).

The pain is – as I mentioned in my last post – that we cannot use the same SQLite database engine for both platforms. Therefore the sharing of the data access classes is for example possible via linking the same code classes in separates data layer libraries:

 image

To capsulate the data classes, which is possible by using the new portable library type in Visual Studio, you have to use a little “dirty” trick. If you install sqlite-net via Nuget you got two classes SQLite.cs and SQLiteAsync.cs for asynchronous database access. In the class SQLite.cs you will find the property attributes for your data classes. If you want to put them in a reusable library, you have to comment the necessary attribute definitions and copy them to your data classes library. Really dirty – but it works .-)

So in my portable library I have also a SQLite.cs file with only the primary attribute definition (I need no more) in the SQLite namespace:

using System;
 
namespace SQLite
{
    [AttributeUsage(AttributeTargets.Property)]
    public class PrimaryKeyAttribute : Attribute
    {
    }
}

The whole demo project you could find on my SkyDrive: http://sdrv.ms/RFOrNQ

5 Kommentare:

  1. Dieser Kommentar wurde vom Autor entfernt.

    AntwortenLöschen
  2. SQLite is not truly crossplatform due to dependency on native dll. Writing WinRT App you have to maintain/publish/update/test 3 versions - x86, x64 and ARM.

    I see a better alternative. Check out an article on Nokia Developer Wiki: Portable Local Storage

    AntwortenLöschen
  3. This is simply what I was searching for, for my Wp7 undertaking, much obliged!windows mobile app // iPhone app maker // mobile app developers

    AntwortenLöschen