Windows 10开发 - SQLite数据库

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

如果您熟悉数据库的类型,例如SQL Server或Oracle数据库任何应用程序,那么它很容易理解 SQLite数据库.

什么是SQLite?

SQLite是一个软件实现自包含,服务器少,零配置,事务性SQL数据库引擎的库.

重要功能是 :

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

  • SQLite的源代码是开源的.

  • 由于其便携性和占地面积小,它对游戏和移动应用程序开发产生了巨大影响.

SQLite的优点

以下是SQLite&minus的优点;

  • 这是一个非常轻量级的数据库.

  • 它是platfo rm独立,适用于所有平台.

  • 内存占用少.

  • 可靠.

  • 无需任何设置和安装.

  • 它没有依赖关系.

使用您的通用Windows平台(UWP)应用程序中的SQLite ,您需要按照下面给出的步骤.

  • 创建一个名为 UWPSQLiteDemo 的新通用Windows空白应用.

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

UWP SQLite Demo

  • 选择Extensions and Updates后,将打开以下窗口.

UWP SQLite Extensions and Updates

  • 现在从左侧窗格中选择在线选项并搜索SQLite.

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

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

UWP SQLite管理控制台

  • 在软件包管理器控制台中编写以下命令,然后按Enter键执行此命令 :

 Install-Package SQLite.Net-PCL


UWP SQLite控制台命令

  • 现在右键单击解决方案资源管理器中的参考,然后选择添加参考.

UWP SQLite Add References

  • 将打开以下对话框.

UWP SQLite Dialog

  • 从左侧窗格中选择扩展程序通用Windows 下,在中间窗格中检查SQLite for Universal App Platform,然后单击"确定".

  • 现在您已准备好了并在您的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; 
}

让我们了解如何创建数据库,表以及如何在数据库的帮助下插入和检索数据简单的例子.我们将添加Name和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>

下面给出了事件的C#实现和 SQLite数据库.

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添加按钮

现在点击检索按钮.您将在文本块上看到以下数据.

UWP SQLite Retrieve

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

 
 [PrimaryKey,AutoIncrement] 
 public int Id {get;组; }