UWP開發-在UWP中使用sqlite


sqlite是一種輕量級的數據庫,對於一些資源緊張又需要數據庫的開發非常好用。

SQLite 是一個開源的無服務器嵌入式數據庫。 這些年來,它已作為面向存儲在許多平台和設備上的數據的主要設備端技術出現。 通用 Windows 平台 (UWP) 支持並建議使用 SQLite 實現跨所有 Windows 10 設備系列的本地存儲。

SQLite 最適用於手機應用、面向 Windows 10 IoT 核心版(IoT 核心版)的嵌入式應用程序,以及作為企業關系數據庫服務器 (RDBS) 數據的緩存。 它將滿足大多數本地數據訪問需求,除非這些數據需要大量的並發寫入,或除了與大多數應用不同的大數據規模方案。

在媒體播放和游戲應用程序中,SQLite 還可用作文件格式來存儲目錄或其他資源(例如游戲級別),可以從 Web 服務器按原樣下載該文件格式。

這里是微軟的官方文檔:https://msdn.microsoft.com/zh-cn/windows/uwp/data-access/sqlite-databases

 

1.首先,下載和安裝sqlite,這里是下載地址:http://sqlite.org/download.html

對於UWP開發,應該下載Universal Windows Platform版本,就是這個:

下載完成后,安裝即可。

 

2.接着添加對sqlite的引用:

在解決方案中,右擊引用-》添加引用-》引用管理器中,打開Universal Windows標簽頁-》選中SQLite for Universal Windows Platform,確定。

如果這里沒有SQLite for Universal Windows Platform,可能版本不對,也可能你沒有正確執行第一步。

 

3.如果你想使用C#開發,當然還要添加SQLitePCL引用。右擊引用-》管理Nuget程序包-》在“瀏覽”標簽頁下搜索SQLite.Net-PCL,安裝SQLitePCL。

安裝完成后,你的引用下面應該是類似這樣的,注意,一定有紅線畫的兩個引用。

 

4.現在,你可以開發了。

新建一個類,就Book.cs吧,我這里只是個示例,因為后面用到一下

    public class Book
    {
        [PrimaryKey,AutoIncrement]
        public int Id { get; set; }

        [MaxLength(64)]
        public string Name { get; set; }

        public string Description { get; set; }
    }

注意添加using SQLite.Net.Attributes;引用

上面的[PrimaryKey,AutoIncrement]是對Id的約束屬性,表示Id是主鍵且自增;[MaxLength(64)]則是表明Name最大長度為64。不懂的谷歌一下,屬於SQL的基礎內容。

 

對數據庫操作不外乎增刪改查,這里提供一個工具類供參考。

using SQLite.Net;
using SQLite.Net.Platform.WinRT;
using SqliteSample.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace SqliteSample
{
    public class Dal
    {
        private static string dbPath = string.Empty;
        private static string DbPath
        {
            get
            {
                if (string.IsNullOrEmpty(dbPath))
                {
                    dbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Sqlite.db");
                }

                return dbPath;
            }
        }

        private static SQLiteConnection DbConnection
        {
            get
            {
                return new SQLiteConnection(new SQLitePlatformWinRT(), DbPath);
            }
        }

        public static void CreateDatabase()
        {
            // 創建連接
            using (var db = DbConnection)
            {
                //創建Book表
                var c = db.CreateTable<Book>();

                //插入示例數據
                Book book = new Book();
                book.Id = 1;
                book.Name = "C# in depth";
                book.Description = "good book";
                db.InsertOrReplace(book);
            }
        }

        /// <summary>
        /// 刪除記錄
        /// </summary>
        /// <param name="book">實體</param>
        public static void DeleteBook(Book book)
        {
            using (var db = new SQLiteConnection(new SQLitePlatformWinRT(), DbPath))
            {
                db.Execute("DELETE FROM Person WHERE Id = ?", book.Id);
            }
        }

        /// <summary>
        /// 獲取全部的記錄
        /// </summary>
        /// <returns>全部實體集合</returns>
        public static List<Book> GetAllPersons()
        {
            List<Book> models;

            using (var db = new SQLiteConnection(new SQLitePlatformWinRT(), DbPath))
            {
                models = (from p in db.Table<Book>()
                          select p).ToList();
            }

            return models;
        }

        /// <summary>
        /// 獲取記錄
        /// </summary>
        /// <param name="Id">實體Id</param>
        /// <returns>實體</returns>
        public static Book GetBookById(int Id)
        {
            using (var db = new SQLiteConnection(new SQLitePlatformWinRT(), DbPath))
            {
                Book m = (from p in db.Table<Book>()
                            where p.Id == Id
                            select p).FirstOrDefault();
                return m;
            }
        }

        /// <summary>
        /// 創建或更新一條記錄
        /// </summary>
        /// <param name="book">實體</param>
        public static void SaveBook(Book book)
        {
            using (var db = new SQLiteConnection(new SQLitePlatformWinRT(), DbPath))
            {
                if (book.Id == 0)
                {
                    // 新建
                    db.Insert(book);
                }
                else
                {
                    // 更新
                    db.Update(book);
                }
            }
        }
    }
}

Model就是上面的Book.cs

 

5.結語

sqlite還是挺好用的,除了微軟提供的ApplicationData儲存用戶數據之外,sqlite可以儲存圖片什么的,不像自帶的應用存儲對類型有限制。

可以交月報了嗎?2333考試啊,一看都31號了,都忘交了好幾次月報了,再不交就對不起組織了2333,本來還有幾篇,沒想到好浪費時間啊,滾去復習啊 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM