在Windows Phone上使用SQLite,並擷取和編輯模擬器中的資料庫檔案

來源:互聯網
上載者:User

大家知道,在移動端實現資料的離線採集,最重要的一點就是在行動裝置上臨時儲存資料,那麼需要行動裝置上提供對資料庫(不管是本地原生還是第三方)的支援。SQLite是一款輕型的資料庫,目前已經在很多嵌入式產品中使用了它,iOS和Android等主流的移動平台也對它提供了支援,關於SQLite資料在此不再贅述,有需要的同學自己Google一下。本文主要研究了以下2點:

1、在Windows Phone上使用SQLite資料庫。

其實SQLite的使用很簡單,學過或瞭解SQL語句的同學都非常容易上手,在這部分我將詳細講述在WP上使用SQLite的詳細步驟,包括安裝配置和詳細代碼,供初學者參考。

2、擷取隔離儲存區 (Isolated Storage)區中的資料庫檔案。

隔離儲存區 (Isolated Storage)(IsolatedStorage)是Silverlight一個特色,它是Silverlight的虛擬檔案系統,關於虛擬檔案系統的概念,大家也可以自己百度,Windows Phone 7是基於Silverlight,它的檔案系統也是IsolatedStorage,使用SQLite在WP端儲存離線資料時,資料庫檔案就存放在隔離儲存區 (Isolated Storage)區,那麼這給我們開發人員帶來的一個困擾是:我們似乎無法看到模擬器中的資料庫檔案,不知道每次操作時資料庫中發生了怎麼樣的改變,不利於調試,因此,這部分給大家介紹兩種工具,分別用來從真機/模擬器的隔離儲存區 (Isolated Storage)中取出資料檔案和可視化的操作資料檔案。

先看第一部分。

1、在Windows Phone上使用SQLite資料庫1)擷取SQLite類庫

1、從網站下載Sqlite Clientfor Windows Phone 7,截止目前,最新版本為0.6.1,:http://sqlitewindowsphone.codeplex.com/;

2、下載後解壓檔案夾內容如下:

3、編譯Community.CsharpSqlite.WP程式,擷取Community.CsharpSqlite.WP.dll類庫檔案。

2)在Windows Phone上使用SQLite

1、建立windows Phone應用程式,目標平台選擇wpos 7.1;

2、添加對1)中產生的Community.CsharpSqlite.WP.dll檔案的引用;

3、在建立的程式首頁面中設計4個按鈕,分別用來建立、填充、清除和關閉資料庫,如下:

4、在後台代碼中添加SQLite類庫的引用:

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;

 

5、添加SQLite資料庫連接變數:

namespace SQLiteTest

{

    public partial class MainPage : PhoneApplicationPage

    {

        SQLiteConnection mySQLiteDB = null;

 

        // 建構函式

        public MainPage()

        {

            InitializeComponent();

        }

    }

}

 

6、給“Open”按鈕添加事件,建立並開啟資料庫:

private
void btnOpen_Click(object sender,
RoutedEventArgs e)

        {

            if (mySQLiteDB ==
null)

            {

                mySQLiteDB = new
SQLiteConnection("TestSQLiteDB");

                mySQLiteDB.Open();

 

                btnOpen.IsEnabled = false;

                btnClose.IsEnabled = true;

                btnClear.IsEnabled = false;

                btnPopulate.IsEnabled =
true;

            }

        }

 

7、接下來,需要建立表,並往表中填充資料,使用SQLiteCommand對象實現:

private
void btnPopulate_Click(object sender,
RoutedEventArgs e)

        {

            //建立表RegisteredStudents,有3個屬性:id、姓名、學號

            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();

            }

 

            btnPopulate.IsEnabled = false;

            btnClear.IsEnabled = true;

        }

 

8、清空表中的資料,同樣使用SQLiteCommand對象,代碼如下:

private
void btnClear_Click(object sender,
RoutedEventArgs e)

        {

            SQLiteCommand cmd = mySQLiteDB.CreateCommand("drop table RegisteredStudents");

            int i = cmd.ExecuteNonQuery();

 

            btnPopulate.IsEnabled = true;

            btnClear.IsEnabled = false;

        }

 

 

9、斷開資料庫連接,關閉資料庫,如下:

private
void btnClose_Click(object sender,
RoutedEventArgs e)

        {

            if (mySQLiteDB !=
null)

            {

                mySQLiteDB.Dispose();

                mySQLiteDB = null;

                btnOpen.IsEnabled = true;

                btnPopulate.IsEnabled =
false;

                btnClear.IsEnabled = false;

                btnClose.IsEnabled = false;

            }

        }

運行程式,點擊open可以在WP的模擬器的隔離儲存區 (Isolated Storage)空間中建立名為“TestSQLiteDB”資料庫,點擊populate按鈕可以為其填充資料,點擊clear可以清空資料庫中的資料,close關閉資料庫連接。但是所有這些執行成功的操作在資料庫中引起了怎樣的變化,我們不得而知,所以接下來我們進入到第二步,如何取到模擬器中的資料庫檔案。

2、擷取和操作隔離儲存區 (Isolated Storage)區中的資料庫檔案1)擷取資料庫檔案

Windows Phone Power Tools工具可以直接連接模擬器/真機,讀取隔離儲存區 (Isolated Storage)空間中的資料(參考文章)。下載該工具,具體使用方法很簡單,如:

選擇真機或模擬器串連

串連成功後,在File Browser中可以看到,已經取到了該應用程式的隔離儲存區 (Isolated Storage)區以及其中的資料庫檔案,點擊“get”,將該資料檔案儲存到本地:

2)可視化操作資料庫檔案

通過Windows Phone PowerTools將資料庫檔案匯出到電腦中,這個時候我們可以依靠另一種非常好用的視覺化檢視“SQLite-Manager”來操作和管理資料庫檔案了。SQLite-Manager協助我們管理和建立與資料庫相關的東西,如database、tables、views等,SQLite-Manager是一個外掛程式,可以在Firefox瀏覽器中安裝:

安裝好後,在Firefox的瀏覽器菜單中啟動SQLite Manager:

接下來,我們就可以非常方便的可視化的操作資料庫了:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.