windows phone 8.1開發SQlite數據庫操做詳解

原文出自:http://www.bcmeng.com/windows-phone-sqlite1/php

本文小夢將和你們分享WP8.1中SQlite數據庫的基本操做:(最後有整個示例的源碼)(但願能經過本站廣告支持小夢,謝謝!)sql

  • 創建數據庫
  • 增長數據
  • 刪除數據
  • 更改數據
  • 查詢數據

(注:爲了讓每一個操做都能及時顯示在UI上,因此進行了數據綁定.數據綁定會在後面文章專門講解,先給出數據類Note,表明一個筆記.含有Name 和content  屬性.其代碼以下:若是不清楚,我會在以後講解):數據庫

namespace SQlite
{
    public class Note : INotifyPropertyChanged
    {
           private int id;
           [AutoIncrement, PrimaryKey]
            public int ID
            {
               get { return id; }
                set
            {
                if (value!=id)
                {
                    id = value;
                    RaisePropertyChanged("ID");

                }
            }
           }
           private string name;
            [MaxLength(30)]
           public string Name
           {
               get { return name; }
               set
               {
                   if (value != name)
                   {
                       name = value;
                       RaisePropertyChanged("Name");

                   }
               }
           }

            private string content;
            [MaxLength(300)]
            public string Content
            {
                get { return content; }
                set
                {
                    if (value != content)
                    {
                        content = value;
                        RaisePropertyChanged("Content");

                    }
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;
            protected void RaisePropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    }
}

windows phone 8.1開發SQlite-創建數據庫

private SQLiteAsyncConnection GetConn()
        {

            return new SQLiteAsyncConnection(ApplicationData.Current.LocalFolder.Path + "\\note.db");

        }

        private  async void createButton_Click(object sender, RoutedEventArgs e)
        {

            SQLiteAsyncConnection conn = GetConn();

             await conn.CreateTableAsync<Note>();

            await new MessageDialog("建立數據庫成功!").ShowAsync();
        }

windows phone 8.1開發SQlite-增長數據

SQLiteAsyncConnection conn = GetConn();

            await conn.InsertAsync(new Note { Name = "小夢", Content = "小夢我想你" });
            await conn.InsertAsync(new Note { Name = "小夢", Content = "小夢我愛你" });
            await conn.InsertAsync(new Note { Name = "小夢", Content = "小夢我喜歡你" });
            await conn.InsertAsync(new Note { Name = "小夢", Content = "小夢我恨你" });
            await conn.InsertAsync(new Note { Name = "小夢", Content = "小夢我打你" });

            List<Note> notelist = await conn.Table<Note>().ToListAsync();
            notes = new ObservableCollection<Note>(notelist);
            listBox.ItemsSource = notes;
            await new MessageDialog("增長數據成功!").ShowAsync();

windows phone 8.1開發SQlite-刪除數據

SQLiteAsyncConnection conn = GetConn();
            var query =  await conn.Table<Note>().FirstAsync();
            for (int i = 0; i < notes.Count;i++ )
            {
                if (notes[i].ID == query.ID)
                {
                    notes.RemoveAt(i);
                    break;
                }
            }

            await conn.DeleteAsync(query as Object);
            listBox.ItemsSource = notes;
            await new MessageDialog("刪除數據成功!").ShowAsync();

windows phone 8.1開發SQlite-修改數據

SQLiteAsyncConnection conn = GetConn();
            for (int i = 0; i < notes.Count; i++)
            {
                if (notes[i].Name=="小夢")
                {
                    notes[i].Content = "小夢我愛你";

                }
            }
            var query = conn.Table<Note>().Where(x => x.Name=="小夢");

            var result = await query.ToListAsync();

            foreach (var item in result)
            {

                item.Content = "小夢我愛你";

                await conn.UpdateAsync(item);
            }

            await new MessageDialog("更新數據成功!").ShowAsync();

windows phone 8.1開發SQlite-查詢數據

SQLiteAsyncConnection conn = GetConn();

            var query = conn.Table<Note>();

            var result = await query.ToListAsync();
            notes = new ObservableCollection<Note>(result);
            listBox.ItemsSource = notes;
            await new MessageDialog("查詢數據成功!").ShowAsync();

windows phone 8.1開發SQlite數據庫操做詳解源碼下載:

點我下載!windows

相關文章
相關標籤/搜索