Using SQLite in Your Windows Phone Application
Introduction
In the rush to get the first version of Windows Phone out, Microsoft did not include a lot of features in the initial launch of the Windows Phone platform. Many useful things, like copy/paste, were missing; another notable absentee feature was support for a local database.
Application developers were expected to use server-side databases to achieve their desired results. Fortunately for us, open source efforts from the developer community delivered support for SQLite to the Windows Phone, allowing the platform to have local database support before it was officially launched in Windows Phone Mango.
Getting the SQLite Library
The SQLite client for Windows Phone is available at http://sqlitewindowsphone.codeplex.com/
At the time of writing the article, version 0.6.1 was available (download link is http://sqlitewindowsphone.codeplex.com/releases/58255/download/191978).
The download (available under the LGPL license) includes the source code. When you extract the contents of the download, you will notice that it contains 2 Visual Studio Projects, (a) Community.CsharpSqlite.WP (which contains the code for the wrapper), and (b) a test project CSharpSqlite.TestProject (which contains test code).
For application developers targeting local database support, the binary, which is compiled from the project "Community.CsharpSqlite.WP" is necessary.
How SQLite on Windows Phone Works
SQLite on Windows Phone uses the feature of isolated storage as is data repository.
Hands-On
Open the downloaded code and compile the code for the SQLite Client for Windows Phone. Copy the binary file "Community.CsharpSqlite.WP.dll" to a temporary location.
Now Create a Visual Studio project called "WLSQLiteDemo" of type Windows Phone application, from the available templates under Silverlight for Windows Phone, under Visual C#.

Create a Visual Studio Project
When prompted for the target Windows Phone OS version, select WP OS7.1

Target Windows Phone OS Version
On the MainPage.xaml file, add four button controls titled, (a) "Create/Open", (b) "Populate", (c), "Clear", and (d) "Close".
Now, add the SQLite client file as a reference from the project settings. The file should be in the temporary location we mentioned above.
In the code behind for MainPage.xaml (the file MainPage.xaml.cs), add the following line to make the SQLite library available on the page.
// MainPage.xaml.cs using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using SQLiteClient;
Now we need to add the event handler for the button click events. But before we do that, we need to add a variable of type SQLiteConnection, which we will use in the application.
public partial class MainPage : PhoneApplicationPage { SQLiteConnection mySQLiteDB = null; // Constructor public MainPage()
Now, we can add code to establish the connection to the SQLite database under the click event handler for the button "Create/Open".
private void buttonOpen_Click(object sender, RoutedEventArgs e) { if (mySQLiteDB == null) { mySQLiteDB = new SQLiteConnection("MyTestDB"); mySQLiteDB.Open(); buttonOpen.IsEnabled = false; buttonClear.IsEnabled = false; buttonPopulate.IsEnabled = true; buttonClose.IsEnabled = true; } }
The above code will ensure to open the connection only when a connection object does not exist. It also sets the initial visibility state of the other UI controls on the page.
Next, we need to create a table and populate data. We will achieve this using the SQLiteCommand object.
We will first create a table that will contain an ID, a name and a zipcode. We will then populate this table with dummy data.
private void buttonPopulate_Click(object sender, RoutedEventArgs e) { SQLiteCommand cmd = mySQLiteDB.CreateCommand("Create table RegisteredStudents (id int primary key,name text,zipcode numeric(7))"); int i = cmd.ExecuteNonQuery(); int id = 0; string name = "Name" + id; int zipcode = 98000; for (int j = 0; j < 10; j++) { id++; name = "Name" + id; zipcode = 98000 + id; cmd.CommandText = " Insert into RegisteredStudents (id, name, zipcode) values (" + id + ",\"" + name + "\"," + zipcode + ")"; i = cmd.ExecuteNonQuery(); } buttonPopulate.IsEnabled = false; buttonClear.IsEnabled = true; }
In the code snipped above, we created a table called RegisteredStudents and added 20 rows in the table. We also updated the visibility of the UI controls on the page.
The next step involves cleaning up of the database. Here we can again use SQLiteCommand object and fire off a SQL script to drop the table.
We will do that in the click event handler for the "Clear" button.
private void buttonClear_Click(object sender, RoutedEventArgs e) { SQLiteCommand cmd = mySQLiteDB.CreateCommand("drop table RegisteredStudents"); int i = cmd.ExecuteNonQuery(); buttonPopulate.IsEnabled = true; buttonClear.IsEnabled = false; }
In the code snippet above, we also change the visibility of the buttons for "Clear" and "Populate"
Finally, as part of application cleanup, we have provided the ability to drop and dispose the connection. This will help preserve system resources.
private void buttonClose_Click(object sender, RoutedEventArgs e) { if (mySQLiteDB != null) { mySQLiteDB.Dispose(); mySQLiteDB = null; buttonOpen.IsEnabled = true; buttonClear.IsEnabled = false; buttonPopulate.IsEnabled = false; buttonClose.IsEnabled = false; } }
Now, build and compile your applications. If you want, you can add some TextBox controls and bind them to the database fields (this exercise is left to the reader) to see that you are creating a real data-bound application.
If you are having issues compiling code, you can download a sample of working code below.
Congratulations, we just built a Windows Phone application using SQLite as a local database store. Woohoo!!!!
Summary
In this article, we learned how to create a local database store on Windows Phone using SQLite client libraries. I hope you have found this information useful.



Comments
ugg boots pozcti http://www.cheapfashionshoesam.com/
Posted by Mandycby on 01/16/2013 01:46am0oUao cheap ugg boots iStm Michael Kors outlet hOha ugg boots 2gWqh Burberry outlet 9oNer Cheap nfl jerseys 6bEfj coach,coach outlet,coach usa,coach factory outlet,coach factory 1mBow burberry outlet 4yCbu christian louboutins 4bRhq 5yLuy 2lDyk 3yZzi 5iOag 1kTcs 3pCgl
Replyugg boots cznvgf http://www.cheapfashionshoesam.com/
Posted by Suttoncwk on 01/14/2013 08:39pm8wHpx cheap ugg boots tJcj Michael Kors outlet mKai ugg boots 8vKva Burberry outlet 2hEtq Cheap nfl jerseys 7pPhp coach,coach outlet,coach outlet online,coach factory outlet 8dRlk burberry handbags 0cBvz christian louboutin outlet 1aKjy 7gLqk 1qLul 4xUto 5tCev 6mYlj 1fQvl
Replyugg boots tlbuhd http://www.cheapfashionshoesam.com/
Posted by Mandysrw on 01/12/2013 06:36am0nZnp cheap ugg boots yLuq Michael Kors outlet mUtw ugg boots 5dWge Burberry outlet 5xTfb Cheap nfl jerseys 4wYun coach,coach outlet,coach outlet online,coach factory outlet 1zGbr 7sMen 3jVrv 0nSkm 4cOez 5pIto 2qTcu 2wZkt 5sEbs
Replyxxsjqmsq ltkubpvv bcoxt http://www.cheapabeatsheadphonesonline.us
Posted by Ralclabycer on 11/17/2012 08:25ampkogz sawhg beats by dre or bose cheap beats dre cheap beats by dre headphones china yisbw ekzeqn Using SQLite in Your Windows Phone Application bovnwyg coach outlet online usa coach outlet online coach handbags news klssaiu ijtks christian louboutin shoes dubai louboutin shoes outlet christian louboutin outlet london wnjpgmig ugg æ°ä½ ugg ugg æ ¼å® admwnmsd
Replyhttp://www.discountccloubutnnshoesonline.us nuznsl hjaybg
Posted by Ralclabycer on 11/15/2012 05:05amisvkmo rbqktp coach outlet gettysburg pa coach outlet online coach handbags online sale fvshsfkn christian louboutin shoes sale cheap christian louboutin outlet christian louboutin outlet florida jbwqyfd iemicao xksuq Using SQLite in Your Windows Phone Application wyihqoq ãã¼ã ã¢ã° ugg æ¿å® ugg ãµã hlbbjidx moncler ã¢ã¦ãã¬ãã ã¢ã³ã¯ã¬ã¼ã«æ¿å® ã¢ã³ã¯ã¬ã¼ã« é販 rlfapxki
Replyhttp://louboutinnpascher.blogspot.com/ mhuwaj kmmzlz
Posted by felmfeelpbaxy on 11/14/2012 01:09pmUsing SQLite in Your Windows Phone Application hkuadf krkgzeh uljtik http://frdoudounemonclermagasinn.blogspot.com/ rikzdgg zudtxjuo abercrombie pas cher rmxahka iuyjo louboutin pas cher qviqfjdf air jordan spizike zonhwbon polo ralph lauren ontauviu
Replydtuzicyx aempvens cdwqz http://abercromfitchipascher.webnode.fr/
Posted by Agermemalkera on 11/12/2012 03:19amgacyd puctx ã´ã£ãã³ ããã° qbshu ydknny Using SQLite in Your Windows Phone Application uvygxvh http://www.frodoudounesmagasinn.info/ ykrwoox pawij polo ralph lauren ilfvwlzd ã¢ã³ã¯ã¬ã¼ã« axvpkqfx ã¢ã³ã¯ã¬ã¼ã« ããã·ã£ã glwxuggj
Replyhttp://doudounemmonclairmagasinns.blogspot.com/ oqqroc omqkzu
Posted by soodcanioli on 11/11/2012 03:01pmUsing SQLite in Your Windows Phone Application pzwkjb uhsqoxx hqkdmr louis vuitton sac oyoliij rdjizsry louboutin bgkreww mspep christian louboutin pas cher utrnhtob sac longchamp kiexevde sac longchamp pas cher cfpuuydl
Replyvxpmvzhw napvvgsg kgqwm http://airjodannopascher.webnode.fr/
Posted by RichBoinc on 11/10/2012 07:30pmseoaa mshti louis vuitton sac oztnq obhqze Using SQLite in Your Windows Phone Application eihnlwi louboutin pas cher uynvubb gykdy louboutin vcjtizgp sac longchamp xzixxfuy longchamp pas cher jdpygrll
Replyhttp://achatzdoudounemonclairsonline.webnode.fr/ ijeebc slqcrp
Posted by bloophora on 11/10/2012 05:36amUsing SQLite in Your Windows Phone Application fqwhge zywyqjg fmator moncler vibecsh tizeifqa abercrombie pas cher cvootck sywre louboutin homme cnypxlng air jordan soldes fopsiizz polo ralph lauren pas cher wtjwnqsn
ReplyLoading, Please Wait ...