Partager via


Using SQLite with Windows Phone 8

 Today we are going to look at how we can use SQLite for Windows Phone 8.

There are many posts out there that serves the purpose, but only half of it, today we will resolve the issues and would make sure the project runs fine.

Building the Solutions

to build the solution and run the attached sample you will need visual studio 2012 or higher running on windows 8 Professional x64. The Computer will also need to have a microprocessor that Suppots SLAT (Secondary Level Address Translation)  

Step1

Create a windows Phone Project in Visual Studio

In order to integrate SQLite in Windows Phone, you need to have SQLite for Windows Phone. and that can be installed from Tools > Extensions and Updates.

Once installed, right click on References with in your project, and select Add reference

On the Immediate windows, select windows Phone 8.1 > Extensions > SQLite for Windows Phone.

The Reference has been added to the project. Build the Project and you will get an Error and exclamation sign next to SQlite for windows Phone

 

The Error says to change the target processor Architecture through the Configuration Manager. 

To do that, right click Solution and select Configuration Manager, and change the Active Solution Platform from Any CPU to ARM, as shown in the picture below

Rebuild the Solution again and the Problem should go away.

Next, we need to Add Sqlite-net from Nuget.

Right Click References and Select Manage Nuget Packages. In the Immediate windows Search for Sqlite-net, Make sure your Internet is working fine.

This will Add two C# files to the Solution

Next, we are going to Add a Project to our Solution that i have Added as an attachment, you can also get this from Github by searching Sqlite-net-wp8

Right click on the Solution, select Add > Existing Project, Add the Sqlite-net-wp8 C++ project that i have added as an attachment. Your Project will look like this.

Now, From windows Phone project, Right Click References and select Add Reference

in the Immediate window, Select Solution and Select SQlite

This will add, a Reference to your windows Phone project for the SQlite C++ project.

 Build the Project and you will receive three errors as shown.

Double click on the error and it will take you to SqLite.cs, 

it is because you need to add USE_WP8_NATIVE_SQLITE to your Conditional Symbol, to do that right click your Windows Phone Project and Select Properties

in the Properties Windows, Select Build from the Left Section, and in the Conditional Compilation Symbols, put a semicolon and paste USE_WP8_NATIVE_SQLITE.

Save All and Build the Project, if you have done everything rite, your project will compile just fine.

Download the Attachment that contains the working project and Sqlite-net-wp8 from Github.  

Solution and C++ project.rar

Comments

  • Anonymous
    April 28, 2014
    Configuring this way, the project then runs only on devices. For Running it on Emulator, we need to change a last step a bit. Change Configurations to All Configurations and also change Platform to All Platforms. The Conditional compilation symbols should be copied before changing the settings and then paste them there after making changes. Now your project is good to run on emulator :)

  • Anonymous
    September 03, 2014
    Thanks for your share, that was a great tutorial but here is the thing, why would using SQLite that much hard in a project? This doesn't makes any sense!

  • Anonymous
    September 08, 2014
    it was really helpfull document thank you. i am trying to use sqlite in my project . i have sqlite database with lots of data in it and i am trying to get that data from database in wp8 app but i got nothing here is m code if you help me it will be greate when i run the code the upplication returns me nothing but vaktija2.db is full of data . in the example nokia developers provide(developer.nokia.com/.../How_to_use_SQLite_in_Windows_Phone) you can create new table and add new row to table add list these rows but when i just want to list rows from database i can not get them. i mean i can list rows that are entered from application but i can not list rows that are already in database public static string DB_PATH = Path.Combine(Path.Combine(ApplicationData.Current.LocalFolder.Path, "vaktija2.db"));                private SQLiteConnection dbConn;        public MainPage()        {            InitializeComponent();        }        protected override void OnNavigatedTo(NavigationEventArgs e)        {                      dbConn = new SQLiteConnection(DB_PATH);            SQLiteCommand sqlComm = new SQLiteCommand(dbConn);            sqlComm.CommandText = "select * from vaktija where _id= 1";            List<vaktija> retrievedTasks = sqlComm.ExecuteQuery<vaktija>();            foreach (var t in retrievedTasks)            {                a.Text = t.data.ToString();            }        }        protected override void OnNavigatedFrom(NavigationEventArgs e)        {            if (dbConn != null)            {                              dbConn.Close();            }        }        }    public sealed class vaktija    {        [PrimaryKey, AutoIncrement]        public int _id { get; set; }        public string grad { get; set; }        public string data { get; set; }              public override string ToString()        {            return data;        }    }

  • Anonymous
    September 18, 2014
    Hi, I am using VS2012 and while performing "Right click on the Solution, select Add > Existing Project, Add the Sqlite-net-wp8 C++ project that i have " I am getting the error: "A reference to a higher version or incompatible assembly cannot be added to this project ". Hence I am unable to get the "SQLite.dll" reference. Please suggest an alternative or share some SQLite project which can be used with VS2012. Thanks in advance.

  • Anonymous
    September 20, 2014
    System.Data.SQLite, SQLite + LINQ, DataTable etc. for Windows Phone 8! Project and examples for download: sqliteandcsharp.blogspot.com/.../jak-powinna-wygladac-ergonomia-pracy-ze.html

  • Anonymous
    December 09, 2015
    return error on add reference to existing proyect for sqlite-net-wp8-master