Sunteți pe pagina 1din 6

16/9/2556

How to use SQLite in Windows Phone - Nokia Developer Wiki

Categories: Files/Data on Windows Phone Code Examples

How to use SQLite in Windows Phone


This article explains how to add SQLite support in Windows Phone 8 applications and how to start using it.

Introduction
A very common task for most of the applications is to store and access the data. Almost all applications has to store some kind of user data for a later use. This is the reason why mobile platforms provide ways to the developer handle those data; with Windows Phone 8 is not different, it provides the System.IO.IsolatedStorage namespace to access files and/or application settings. This is normally enough for many applications, but a database would be better if the user wants to store data in a more structured way. This post is about the use of the SQLite database to handle those structured data. At the time of writing this post, it is not so easy to start using SQLite in a Windows Phone 8 application. So, the first part of this post will give a workaround to do that. The second part will show how to create a connection, a database, tables and how to store and retrieve data. The first part has to be updated in the future, when the startup of SQLite become easier. Tip: This article covers Windows Phone 8. If you want to develop a database-driven application for Windows Phone 7 devices, then we recommend you use the library sqlitewindowsphone (http://sqlitewindowsphone.codeplex.com/) as described in this post (http://www.codeguru.com/csharp/.net/wp7/usingsqlite-in-your-windows-phone-application.htm) . Tests show it works perfectly on Lumia 800!

Adding SQLite support to the Windows Phone 8 application


This post will use a simple example to install the SQLite support and handle a database. So, the first thing you have to do is to create an empty Windows Phone application solution. I'll assume the name of the application that you created as "SqliteForWP8". First of all, I have to say that everything I've done to add SQLite support in "SqliteForWP8" application was based on this post (http://wp.qmatteoq.com/working-withsqlite-in-windows-phone-8-a-sqlite-net-version-for-mobile/) written by qmatteoq . I'll just filter his post and put here only the needed steps. If you want more details about why these steps are needed, take a look at the post which is mentioned above since there is a lot of useful information there.

Installing SQLite for Windows Phone SDK


You have to install the SQLite for Windows Phone SDK in the Visual Studio 2012 that you are using. To do that, click on TOOLS -> Extensions and Updates -> Online > Then search for "sqlite for windows phone". It should look like the following picture.

Click on Download. You'll have to restart the Visual Studio after the download is completed. The below image is shown when the download and installation is completed.

developer.nokia.com/Community/Wiki/How_to_use_SQLite_in_Windows_Phone

1/6

16/9/2556

How to use SQLite in Windows Phone - Nokia Developer Wiki

Installing sqlite-net-wp8 wrapper


This wrapper is the C++ bridge between the SQLite for Windows Phone SDK and the sqlite-net NuGet package that we'll install soon. First, you have to clone the following Git repository "https://github.com/peterhuene/sqlite-net-wp8.git". At this point, I'm assuming that you have some knowledge of Git. If you don't know anything about Git, please go to (https://code.google.com/p/tortoisegit/) and install the tortoise Git; it's easy to use and Windows friendly. Now, you add this project to the "SqliteForWP8" solution that we created before: Right click on the Solution (Not the project) -> Click Add -> Existing Project and select the Sqlite.vcxproj from the folder that you cloned previously using Git. This will create the "Sqlite" C++ project in your solution. Now you add a reference to this project in your "SqliteForWP8" project: Right click on the References folder of "SqliteForWP8" project (Not the solution) -> Click on Add Reference -> Solution and select the "Sqlite" project -> Click on OK button. If you have done everything right, at this time you have one solution with two projects inside it. The "Sqlite" C++ project and the "SqliteForWP8" C# project.

Installing sqlite-net package


The sqlite-net package provides the C# classes that you'll use to handle the sqlite database; storing and retrieving data. First, right click on the "SqliteForWP8" project (Not the Solution) -> Click on "Manage NuGet Packages" and search for "sqlite-net" -> Click on "Install" button. At the time of writing this post, the plugin version was 1.0.7 and it was created by "Frank Krueger". Use the following image as a reference to install the correct one.

developer.nokia.com/Community/Wiki/How_to_use_SQLite_in_Windows_Phone

2/6

16/9/2556

How to use SQLite in Windows Phone - Nokia Developer Wiki

You'll probably get this error message: The type or namespace name 'Community' could not be found . It can be fixed by this approach: Create the USE_WP8_NATIVE_SQLITE compilation symbol in the "SqliteForWP8" project. Please pay attention to the configuration and platform that you are creating this symbol since each platform has its own set of compilation symbols. This symbol will tell the sqlite-net package that you are using the SQLite for Windows Phone SDK.

Troubleshootings
"Any CPU" problem
If you get a warning message as shown below, follow these steps to fix it: Right click on the Solution -> Click on Configuration Properties -> Configuration Manager and change the active solution platform to x86 (If you are using an emulator) or ARM (If you are using a Windows Phone 8 device).

Using the database


Finally, we are able to start using a SQLite database to store/retrieve structured data in our application. To keep the code simple, I implemented everything in the MainPage.xaml.cs file. But in real world, a good practice should be to separate the Data layer from UI layer, as MVVM pattern says. The SQLiteConnection class which is shown in the example code is part of the sqlite-net package. It has various methods to control the database but I had shown just a basic usage in this example. To see the complete documentation, go to the https://github.com/praeclarum/sqlite-net/wiki wiki page of the project. If you want to download this example and test it for yourself, click here Media:SQLiteForWP8.zip.
u s i n gS y s t e m ; u s i n gS y s t e m . C o l l e c t i o n s . G e n e r i c ; u s i n gS y s t e m . L i n q ; u s i n gS y s t e m . N e t ; u s i n gS y s t e m . W i n d o w s ; u s i n gS y s t e m . W i n d o w s . C o n t r o l s ; u s i n gS y s t e m . W i n d o w s . N a v i g a t i o n ; u s i n gM i c r o s o f t . P h o n e . C o n t r o l s ; u s i n gM i c r o s o f t . P h o n e . S h e l l ; u s i n gS q l i t e F o r W P 8 . R e s o u r c e s ; u s i n gS Q L i t e ; u s i n gW i n d o w s . S t o r a g e ; u s i n gS y s t e m . I O ;

developer.nokia.com/Community/Wiki/How_to_use_SQLite_in_Windows_Phone

3/6

16/9/2556

How to use SQLite in Windows Phone - Nokia Developer Wiki

n a m e s p a c eS q l i t e F o r W P 8 { p u b l i cp a r t i a lc l a s sM a i n P a g e:P h o n e A p p l i c a t i o n P a g e { / / /< s u m m a r y > / / /T h ed a t a b a s ep a t h . / / /< / s u m m a r y > p u b l i cs t a t i cs t r i n gD B _ P A T H=P a t h . C o m b i n e ( P a t h . C o m b i n e ( A p p l i c a t i o n D a t a . C u r r e n t . L o c a l F o l d e r . P a t h ," s a m p l e . s q l i t e " ) ) ; / / /< s u m m a r y > / / /T h es q l i t ec o n n e c t i o n . / / /< / s u m m a r y > p r i v a t eS Q L i t e C o n n e c t i o nd b C o n n ; / /C o n s t r u c t o r p u b l i cM a i n P a g e ( ) { I n i t i a l i z e C o m p o n e n t ( ) ; / / /D e f i n et h ed a t a b a s ep a t h .T h es q l i t ed a t a b a s ei ss t o r e di naf i l e . } p r o t e c t e do v e r r i d ev o i dO n N a v i g a t e d T o ( N a v i g a t i o n E v e n t A r g se ) { / / /C r e a t et h ed a t a b a s ec o n n e c t i o n . d b C o n n=n e wS Q L i t e C o n n e c t i o n ( D B _ P A T H ) ; / / /C r e a t et h et a b l eT a s k ,i fi td o e s n ' te x i s t . d b C o n n . C r e a t e T a b l e < T a s k > ( ) ; / / /R e t r i e v et h et a s kl i s tf r o mt h ed a t a b a s e . L i s t < T a s k >r e t r i e v e d T a s k s=d b C o n n . T a b l e < T a s k > ( ) . T o L i s t < T a s k > ( ) ; / / /C l e a rt h el i s tb o xt h a tw i l ls h o wa l lt h et a s k s . T a s k L i s t B o x . I t e m s . C l e a r ( ) ; f o r e a c h( v a rti nr e t r i e v e d T a s k s ) { T a s k L i s t B o x . I t e m s . A d d ( t ) ; } } p r o t e c t e do v e r r i d ev o i dO n N a v i g a t e d F r o m ( N a v i g a t i o n E v e n t A r g se ) { i f( d b C o n n! =n u l l ) { / / /C l o s et h ed a t a b a s ec o n n e c t i o n . d b C o n n . C l o s e ( ) ; } } p r i v a t ev o i dI n s e r t _ C l i c k _ 1 ( o b j e c ts e n d e r ,R o u t e d E v e n t A r g se ) { / /C r e a t ean e wt a s k . T a s kt a s k=n e wT a s k ( ) { T i t l e=T i t l e F i e l d . T e x t , T e x t=T e x t F i e l d . T e x t , C r e a t i o n D a t e=D a t e T i m e . N o w } ; / / /I n s e r tt h en e wt a s ki nt h eT a s kt a b l e . d b C o n n . I n s e r t ( t a s k ) ; / / /R e t r i e v et h et a s kl i s tf r o mt h ed a t a b a s e . L i s t < T a s k >r e t r i e v e d T a s k s=d b C o n n . T a b l e < T a s k > ( ) . T o L i s t < T a s k > ( ) ; / / /C l e a rt h el i s tb o xt h a tw i l ls h o wa l lt h et a s k s . T a s k L i s t B o x . I t e m s . C l e a r ( ) ; f o r e a c h( v a rti nr e t r i e v e d T a s k s ) { T a s k L i s t B o x . I t e m s . A d d ( t ) ; } } } / / /< s u m m a r y > / / /T a s kc l a s sr e p r e s e n t i n gt h eT a s kt a b l e .E a c ha t t r i b u t ei nt h ec l a s sb e c o m eo n ea t t r i b u t ei nt h ed a t a b a s e . / / /< / s u m m a r y > p u b l i cs e a l e dc l a s sT a s k { / / /< s u m m a r y > / / /Y o uc a nc r e a t ea ni n t e g e rp r i m a r yk e ya n dl e tt h eS Q L i t ec o n t r o li t . / / /< / s u m m a r y > [ P r i m a r y K e y ,A u t o I n c r e m e n t ] p u b l i ci n tI d{g e t ;s e t ;} p u b l i cs t r i n gT i t l e{g e t ;s e t ;} p u b l i cs t r i n gT e x t{g e t ;s e t ;} p u b l i cD a t e T i m eC r e a t i o n D a t e{g e t ;s e t ;} p u b l i co v e r r i d es t r i n gT o S t r i n g ( ) { r e t u r nT i t l e+" : "+T e x t+"<"+C r e a t i o n D a t e . T o S h o r t D a t e S t r i n g ( )+""+C r e a t i o n D a t e . T o S h o r t T i m e S t r i n g ( ) ; } } }

Preparing the application to publish to the Store


If you don't want to publish the application to the Store, the application is finished and you can jump over this section. For some reason, the current code is not able to create the database file when the application is signed and downloaded from the Store. This is a blocker issue if you want to publish the application to the Store using SQLite databases.

developer.nokia.com/Community/Wiki/How_to_use_SQLite_in_Windows_Phone

4/6

16/9/2556

How to use SQLite in Windows Phone - Nokia Developer Wiki

This section describes a step-by-step guide to create the database file programmatically on first application startup and avoid this issue. Basically, what we are going to do is copy the database file from the installation folder (That is read-only and can be updated in development time) to the local folder (That is read-write and can be updated only in runtime) only on first startup. The given example already follow these steps.

Getting database file using the Isolated Storage Explorer tool


First of all, with the current application running on device or emulator, you have to use the Isolated Storage Explorer tool to get the database file. Normally, this tool is installed in this folder Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Tools\IsolatedStorageExplorerTool. Go to this folder and run one of the below commands: ISETool.exe ts xd 8a40681d-98fc-4069-bc13-91837a6343ca c:\data\myfiles command, if you are running the application on emulator. ISETool.exe ts de 8a40681d-98fc-4069-bc13-91837a6343ca c:\data\myfiles command, if you are running the application on device. The third argument is the application product ID, you can get it in the WMAppManifest.xml file, App tag, ProductId attribute. The product ID of the given example is 8a40681d-98fc-4069-bc13-91837a6343ca. You can see more details about the Isolated Storage Explorer tool here (http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh286408(v=vs.105).aspx) .

Add the database file as a content


Now, if everything is okay, you should have a copy of the Isolated Storage content in the c:\data\myfiles; And the database file should be there too. I'm considering that you are running the example shown in this article, so the file name is sample.sqlite. In order to copy this file to your project, follow these steps: Rigth click on the SqliteForWP8 project; Click on Add option; Click on Add Existing Item option; Select the c:\data\myfiles\sample.sqlite file and click Add . Tip: Remember to change the Copy to Ouput Directory property of the added sample.sqlite file to Copy if newer.

Copy database file to local folder on first startup


Remove the private void Application_Launching(object sender, LaunchingEventArgs e) method from the Application class and use the below one. This method will try to find a database file in the isolated storage, if the file is not found, it'll copy the file from the installation folder to the local folder.
/ /C o d et oe x e c u t ew h e nt h ea p p l i c a t i o ni sl a u n c h i n g( e g ,f r o mS t a r t ) / /T h i sc o d ew i l ln o te x e c u t ew h e nt h ea p p l i c a t i o ni sr e a c t i v a t e d p r i v a t ea s y n cv o i dA p p l i c a t i o n _ L a u n c h i n g ( o b j e c ts e n d e r ,L a u n c h i n g E v e n t A r g se ) { S t o r a g e F i l ed b F i l e=n u l l ; t r y { / /T r yt og e tt h e d b F i l e=a w a i tS t o r a g e F i l e . G e t F i l e F r o m P a t h A s y n c ( M a i n P a g e . D B _ P A T H ) ; } c a t c h( F i l e N o t F o u n d E x c e p t i o n ) { i f( d b F i l e= =n u l l ) { / /C o p yf i l ef r o mi n s t a l l a t i o nf o l d e rt ol o c a lf o l d e r . / /O b t a i nt h ev i r t u a ls t o r ef o rt h ea p p l i c a t i o n . I s o l a t e d S t o r a g e F i l ei s o=I s o l a t e d S t o r a g e F i l e . G e t U s e r S t o r e F o r A p p l i c a t i o n ( ) ; / /C r e a t eas t r e a mf o rt h ef i l ei nt h ei n s t a l l a t i o nf o l d e r . u s i n g( S t r e a mi n p u t=A p p l i c a t i o n . G e t R e s o u r c e S t r e a m ( n e wU r i ( " s a m p l e . s q l i t e " ,U r i K i n d . R e l a t i v e ) ) . S t r e a m ) { / /C r e a t eas t r e a mf o rt h en e wf i l ei nt h el o c a lf o l d e r . u s i n g( I s o l a t e d S t o r a g e F i l e S t r e a mo u t p u t=i s o . C r e a t e F i l e ( M a i n P a g e . D B _ P A T H ) ) { / /I n i t i a l i z et h eb u f f e r . b y t e [ ]r e a d B u f f e r=n e wb y t e [ 4 0 9 6 ] ; i n tb y t e s R e a d=1 ; / /C o p yt h ef i l ef r o mt h ei n s t a l l a t i o nf o l d e rt ot h el o c a lf o l d e r . w h i l e( ( b y t e s R e a d=i n p u t . R e a d ( r e a d B u f f e r ,0 ,r e a d B u f f e r . L e n g t h ) )>0 ) { o u t p u t . W r i t e ( r e a d B u f f e r ,0 ,b y t e s R e a d ) ; } } } } } }

References
Working with SQLite in Windows Phone 8: a sqlite-net version for mobile (http://wp.qmatteoq.com/working-with-sqlite-in-windows-phone-8-a-sqlite-net-version-formobile/) sqlite-net-wp8 (https://github.com/peterhuene/sqlite-net-wp8) Retrieved from "http://developer.nokia.com/Community/Wiki/index.php?title=How_to_use_SQLite_in_Windows_Phone&oldid=210581" This page was last modified on 10 September 2013, at 04:08. 3521 page views in the last 30 days.

developer.nokia.com/Community/Wiki/How_to_use_SQLite_in_Windows_Phone

5/6

16/9/2556

How to use SQLite in Windows Phone - Nokia Developer Wiki

F ea tured links

D istrib utio n

Co mmunity

R eso urces

developer.nokia.com/Community/Wiki/How_to_use_SQLite_in_Windows_Phone

6/6

S-ar putea să vă placă și