Using SQLite in Windows Phone 7/successful to deal with the problem in WP to use like select Chinese

Source: Internet
Author: User
Tags sqlite db
ArticleDirectory
    • Using coolstorage for easy SQLite access on Windows Phone
    • Shipping A SQLite database file with your app

In the current version of Windows Phone, there's no support for a built-in local database. this will be taken care of in the next release of the OS (codename "Mango"), which will be released late this year. mango will have a built-in SQL Ce (-like) database which can be accessed using LINQ to SQL. at the time of writing, there seem to be a few drawbacks you'll have to keep in mind:

    • The backend store is in a non-standard format
    • It's not possible to execute ad-hoc SQL queries (neither DDL nor DML)
    • Shipping A database file with your app is a complicated process

If any of these limitations is a blocker for your application, or you just want to have a local database right now, using SQLite is a great alternative.

Using coolstorage for easy SQLite access on Windows Phone

By far the easiest way to use a local SQLite database in your Windows Phone app is by using the free, open-source vici coolstorage ORM library. there is a specific native build for Windows Phone which has des a driver for SQLite.

Adding coolstorage to your app is pretty straightforward:

    • Option 1: If you have nuget installed in vs2010, addVici coolstoragePackage (available in the nuget Gallery)
    • Option 2: If you don't have nuget, download the binaries from the vici project website and reference both vici. Core. wp7.dll and vici. coolstorage. wp7.dll

Then you add the following line of code somewhere in your initialization code:

Csconfig. setdb ("Mydb. SQLite");//"Mydb. SQLite" is the name of your database file

 

By default, coolstorage will create the database file for you if it doesn't exist. But for this article, we'll assume you already have a database with data (see here)

Coolstorage is a full-blown ORM with support for relations, lazy loading, relation prefetching, etc, so you will be able to use all of that in your Windows Phone app, but for once I won't go into that, because if you want you can also use coolstorage as a lightweight data layer and execute ad-hoc SQL queries.

Here are some examples:

//Execute a SQL insert statementCsdatabase. executenonquery ("Insert into customer (name, city) values (@ name, @ City)",New{Name ="Microsoft", City ="Redmond"});
 //  Execute a select SQL statement and map the result to a class    Class  Queryresult { Public   String  Name;  Public   Int  Numsales;  Public   Decimal  Totalsales;} queryresult [] Results = Csdatabase. runquery <queryresult> (  @" Select name, count (*), sum (S. Total) from salesperson SP inner join sales s on S. salespersonid = sp. ID group by sp. Name  " );
 
//Retrieve a scalar value (for example, the total number of MERs mers)IntNumcustomers = csdatabase. getscalar <Int> ("Select count (*) from customer");

 

So if you're not that crazy about Orm's, or you want to execute some very specific SQL satements, you can do that very easily on Windows Phone with a local SQLite dB.

Shipping A SQLite database file with your app

If you have an existing SQLite database file and you want to ship it with your application, you'll have to make it available inIsolated Storage. This is pretty straightforward, but not always obvious:

First, add your database file to your Visual Studio project and set the build action to "content" (also leave "do not copy ").

Then add the following piece of code before callingCsconfig. setdb (...):

 String Fn = "  Mydb. SQLite  " ; Streamresourceinfo SR = Application. getresourcestream ( New  Uri (FN, urikind. Relative); isolatedstoragefile istorage = Isolatedstoragefile. getuserstoreforapplication ();  If (! Istorage. fileexists (FN )){  Using ( VaR Outputstream = Istorage. openfile (FN, filemode. createnew )){  Byte [] Buffer = New  Byte [ 10000  ];  For  (;;){  Int Read = Sr. Stream. Read (buffer, 0  , Buffer. Length );  If (Read <= 0  )  Break  ; Outputstream. Write (buffer,  0 , Read );}}}  //  Now you can use your database  Csconfig. setdb (FN ); 

 

Having the possibility to ship a SQLite database file with your app is especially useful if you want to share a pre-built database with other mobile platforms, since both iPhone and Android have built-in support for SQLite.

In a next post, I will show how to use the ORM features of coolstorage.

And while you're at it, you might as well use monotouch (and coolstorage) for building the iPhone version of your app, allowing you to reuse all of your business logic and data layer code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.