Windows 10 开发 - SQLite 数据库



在许多应用程序中,存在某些类型的数据,它们之间存在某种关系。这些难以存储在文件中的数据类型可以存储在数据库中。

如果您熟悉各种类型的数据库,例如任何应用程序中的 SQL Server 或 Oracle 数据库,那么理解 **SQLite 数据库** 就非常容易了。

什么是 SQLite?

SQLite 是一个软件库,它实现了一个自包含、无服务器、零配置的事务性 SQL 数据库引擎。

重要特性包括 -

  • SQLite 是世界上部署最广泛的数据库引擎。

  • SQLite 的源代码是开源的。

  • 由于其可移植性和较小的占用空间,它对游戏和移动应用程序开发产生了重大影响。

SQLite 的优点

以下是 SQLite 的优点 -

  • 它是一个非常轻量级的数据库。
  • 它是平台无关的,可以在所有平台上运行。
  • 它具有较小的内存占用。
  • 它是可靠的。
  • 无需任何设置和安装。
  • 它没有任何依赖项。

要在您的通用 Windows 平台 (UWP) 应用程序中使用 **SQLite**,您需要按照以下步骤操作。

  • 创建一个名为 **UWPSQLiteDemo** 的新的通用 Windows 空白应用程序。

  • 转到 **工具** 菜单并选择扩展和更新。将打开以下对话框。

UWP SQLite Demo
  • 选择扩展和更新后,将打开以下窗口。
UWP SQLite Extensions and Updates
  • 现在选择 **联机** 选项,然后从左侧窗格中搜索 SQLite。

  • 下载并安装适用于通用应用平台的 SQLite。

  • 现在,再次转到工具菜单,然后选择 **NuGet 包管理器 > 包管理器控制台** 菜单选项,如下所示。

UWP SQLite Manage Console
  • 在包管理器控制台中写入以下命令,然后按 Enter 执行此命令 -

Install-Package SQLite.Net-PCL 

UWP SQLite Console Command
  • 现在右键单击解决方案资源管理器中的 **引用**,然后选择 **添加引用**。

UWP SQLite Add References
  • 将打开以下对话框。
UWP SQLite Dialog
  • 从 **通用 Windows** 下的左侧窗格中选择 **扩展**,在中间窗格中选中适用于通用应用平台的 SQLite,然后单击确定。

  • 现在,您已准备好开始在您的 UWP 应用程序中使用 SQLite 了。

您可以使用以下代码创建数据库。

string path = Path.Combine(Windows.Storage.ApplicationData.
   Current.LocalFolder.Path, "db.sqlite"); 

SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new 
   SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);

要创建表,您需要使用表名对象调用 **CreateTable** 方法。

conn.CreateTable<Customer>(); 

您可以使用以下代码将数据插入到您的表中。

conn.Insert(new Customer(){
   Name = textBox.Text, 
   Age = textBox1.Text 
});

以下是从表中检索数据的代码。

var query = conn.Table<Customer>(); 
string id = ""; 
string name = ""; 
string age = ""; 
 
foreach (var message in query) { 
   id = id + " " + message.Id; 
   name = name + " " + message.Name; 
   age = age + " " + message.Age; 
}

让我们通过一个简单的示例了解如何创建数据库、表以及如何从数据库中插入和检索数据。我们将添加姓名和年龄,然后我们将从表中检索相同的数据。以下是添加不同控件的 XAML 代码。

<Page 
   x:Class = "UWPSQLiteDemo.MainPage" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:local = "using:UWPSQLiteDemo" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" 
   mc:Ignorable = "d"> 
	
   <Grid Background = "{ThemeResource ApplicationPageBackgroundThemeBrush}" >
      <Button x:Name = "Retrieve" Content = "Retrieve" HorizontalAlignment = "Left"  
         VerticalAlignment = "Top" Margin = "384,406,0,0"  
         Click = "Retrieve_Click"/>
			
      <Button x:Name = "Add" Content = "Add" HorizontalAlignment = "Left"  
         VerticalAlignment = "Top" Margin = "291,406,0,0" Click = "Add_Click"/>
			
      <TextBlock x:Name = "textBlock" HorizontalAlignment = "Left"  
         TextWrapping = "Wrap" Text = "Name" VerticalAlignment = "Top"  
         Margin = "233,280,0,0" Width = "52"/>
			
      <TextBox x:Name = "textBox" HorizontalAlignment = "Left" TextWrapping = "Wrap"  
         VerticalAlignment = "Top" Margin = "289,274,0,0" Width = "370"/>
			
      <TextBlock x:Name = "textBlock1" HorizontalAlignment = "Left"  
         TextWrapping = "Wrap" Text = "Age" VerticalAlignment = "Top"  
         Margin = "233,342,0,0" Width = "52"/>
			
      <TextBox x:Name = "textBox1" HorizontalAlignment = "Left" TextWrapping = "Wrap"  
         VerticalAlignment = "Top" Margin = "289,336,0,0" Width = "191"/>
			
      <TextBlock x:Name = "textBlock2" HorizontalAlignment = "Left"  
         Margin = "290,468,0,0" TextWrapping = "Wrap"  
         VerticalAlignment = "Top" Width = "324" Height = "131"/>
			
   </Grid>
	
</Page>		   

以下是事件和 **SQLite 数据库** 的 C# 实现。

using SQLite.Net.Attributes; 

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.InteropServices.WindowsRuntime; 

using Windows.Foundation; 
using Windows.Foundation.Collections; 

using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation;  

// The Blank Page item template is documented at 
   http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 
 
namespace UWPSQLiteDemo {
 
   /// <summary>
      /// An empty page that can be used on its own or navigated to within a Frame.
   /// </summary>
	
   public sealed partial class MainPage : Page {
      string path; 
      SQLite.Net.SQLiteConnection conn; 
		
      public MainPage(){
         this.InitializeComponent();  
         path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,
            "db.sqlite"); 
         conn = new SQLite.Net.SQLiteConnection(new 
            SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);  
         conn.CreateTable<Customer>(); 
      }
		
      private void Retrieve_Click(object sender, RoutedEventArgs e) { 
         var query = conn.Table<Customer>(); 
         string id = ""; 
         string name = ""; 
         string age = "";  
			
         foreach (var message in query) {
            id = id + " " + message.Id; 
            name = name + " " + message.Name; 
            age = age + " " + message.Age; 
         }
			
         textBlock2.Text = "ID: " + id + "\nName: " + name + "\nAge: " + age; 
      }  
		
      private void Add_Click(object sender, RoutedEventArgs e){ 
       
         var s = conn.Insert(new Customer(){
            Name = textBox.Text, 
            Age = textBox1.Text 
         }); 
			
      } 
   } 
	
   public class Customer {
      [PrimaryKey, AutoIncrement] 
      public int Id { get; set; } 
      public string Name { get; set; } 
      public string Age { get; set; } 
   } 
	
} 

编译并执行上述代码后,您将看到以下窗口。

UWP SQLite Execute

输入 **姓名** 和 **年龄**,然后单击 **添加** 按钮。

UWP SQLite Add button

现在单击 **检索** 按钮。您将在 **文本块** 上看到以下数据。

UWP SQLite Retrieve

ID 字段是主键和自动递增字段,在 Customer 类中指定。

[PrimaryKey, AutoIncrement] 
public int Id { get; set; }
广告

© . All rights reserved.