Professional Documents
Culture Documents
How To Use SQLite in Windows Phone - Nokia Developer Wiki
How To Use SQLite in Windows Phone - Nokia Developer Wiki
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!
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
developer.nokia.com/Community/Wiki/How_to_use_SQLite_in_Windows_Phone
2/6
16/9/2556
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).
developer.nokia.com/Community/Wiki/How_to_use_SQLite_in_Windows_Phone
3/6
16/9/2556
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 ( ) ; } } }
developer.nokia.com/Community/Wiki/How_to_use_SQLite_in_Windows_Phone
4/6
16/9/2556
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.
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
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