WP8.1 use of SQLite Lightweight database development tutorial

Source: Internet
Author: User
Tags sqlite sqlite database

In recent months, WP8 applications have been migrated to WP8.1, and many APIs have been found to have changed.

One of the most impressive and unexpected of all is that "I can't find MessageBox".

The groove, can not find the messagebox ... (100 words omitted here)

Back to the point, in the WP8 phase, Microsoft provides a series of APIs for local database operations, and needs friends to view through: http://msdn.microsoft.com/zh-cn/library/hh202860.

However, in WP8.1 (XAML) These APIs are no longer available, and by searching for some data, they eventually replace some of the previous local database operations APIs with a SQLite version.

Steps

1. Establishment of a WP8.1 project.

2. Draw a simple UI, as shown in figure:

The app has a simple feature: Save the name and age data to a database table.

The bottom half of the app is a list of all the information in the current datasheet that is not currently available so it's not visible.

3. Building Models

 Public class person     {         publicint  ID         {             get;              Set ;         }            Public string Name         {             get;              Set ;         }            Public int Age         {             get;              Set ;         }     }

4. Select the VS menu bar: "Tools"-"Extend and update"--> "Online Search Sqllite", as shown in figure:

Download SQLite for Windows phone8.1, I've already downloaded it here.

5. Reboot vs. after download installation complete.

6. Add a reference to SQLite, as shown in figure:

7. The next compilation occurs with an error, which requires that the compilation schema be adjusted to "ARM" or "X86".

8. The added SQLite reference is developed in C + + and there is no way to use it directly in the project. We also need to add some other packaged classes to the project.

9. Open NuGet Manager, search "Sqlite", install "sqlite-net", as shown in figure:

10. After installation, you will find that there are two more classes in the project, we operate the SQLite database is mainly to use the methods and properties of these two classes. As shown in figure:

11. The next step is to do something really meaningful. First we need to tell SQLite which attribute is the primary key, right?

We take "ID" as the primary key.

ublic class person     {         [PrimaryKey]         [AutoIncrement]         publicint  ID         {             get ;              Set ;         }            Public string Name         {             get;              Set ;         }            Public int Age         {             get;              Set ;         }     }

The attribute "PrimaryKey" indicates that the attribute corresponds to the property as the primary key of the table. "AutoIncrement" indicates that this is a self added column.

12. Create the person table.

/// <summary>         /// Create a person         table /// </summary>         /// <returns></returns>         Private Async Task<sqliteasyncconnection> Createpersontableasync ()         {             varNew sqliteasyncconnection (Localdbpath);              await Conn. Createtableasync<person>();                return conn;         }

Localdbpath is the path to the database file.

We can use the returned "Sqliteasyncconnection" object, the person table to do additions and deletions to check the operation.

13. So far, preparation has been done. You can write the logic of the button click event.

Private Async void adddata_tapped (object  sender, Tappedroutedeventargs e)         {             // construct Person Object             New person ();              = Textboxname.text;              int . Parse (Textboxage.text);               // Insert to Database             var await Createpersontableasync ();              await Conn. Insertasync (person);         }

It should be noted that the Createtableasync method does not overwrite the existing table.

Note

This article mainly helps some people who don't know how to use SQLite in WP8.1.

The code posted in the article does not handle some exceptions, which is not possible in the actual project. The main thing is to make it easier for everyone to see the essentials.

Other database operations I will not enumerate, look at the "Sqliteasync" Class code, 5 minutes to know what can be done through the sqliteasync.

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.