原文:UWP SQLite的使用

1.准备工作

1.首先我们要给项目添加“SQLite for Universal Windows Platform”扩展

点击菜单栏的“工具”-“扩展和更新”,然后在“联机”中搜索

2.安装扩展后我们再添加“SQLite.Net-PCL ”的Nuget程序包

在解决方案管理器中右击项目的“引用”,打开“管理Nuget程序包管理”,在“浏览”中搜索并安装

现在已经准备完成了,可以开始使用了。

2.使用

    先定义一个数据模板,接下来会用到(这里需要"using SQLite.Net.Attributes;")
public class DataTemple
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; } //主键 public string Name { get; set; }
public string Age { get; set; }
}

1.建立数据库

string path = Path.Combine(ApplicationData.Current.LocalFolder.Path, "mydb.sqlite");
SQLiteConnection conn = new SQLiteConnection(new SQLitePlatformWinRT(), path);

2.建表

conn.CreateTable<DataTemple>(); //默认表名同范型参数
//以下等效上面
//conn.CreateTable(typeof(DataTemple));

3.增

增一条:

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

增一堆:

List<DataTemple> dataTemples=new List<DataTemple>();
dataTemples.Add(……);
//添加若干项……
conn.InsertAll(dataTemples);

4.删

    删一条:
conn.Execute("delete from DataTemple where Name = ?", name);

    全删:
conn.DeleteAll<DataTemple>();

5.查

按条件查询(name是string变量):

    方法1:
List<DataTemple> query = conn.Table<DataTemple>().Where(v => v.Name == name).ToList();  
    方法2:
List<DataTemple> query = conn.Query<DataTemple>("select * from DataTemple where Name = ?", name);

查询全部:

    方法1:
List<DataTemple> query = conn.Table<DataTemple>().ToList();
    方法2:
List<DataTemple> query = conn.Query<DataTemple>("select * from DataTemple");

3.Demo

  界面:
<Page
    x:Class="Sqlite.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Sqlite"
    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}">
        
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <Button x:Name = "Add" Content = "添加" Width="80" Margin="0,10,0,0"
                    Click = "Add_Click" HorizontalAlignment="Center"/>
            <Button Name="Select" Content="按名查询" Width="80" Margin="0,10,0,0"
                    Click = "Select_Click" HorizontalAlignment="Center"/>
            <Button x:Name = "Retrieve" Content = "查询全部"  Width="80" Margin="0,10,0,0"
                    Click = "Retrieve_Click" HorizontalAlignment="Center"/>
            <Button Name="Del" Content="按名删除" Width="80" Margin="0,10,0,0"
                    Click = "Del_Click" HorizontalAlignment="Center"/>
            <Button Name="DelAll" Content="全删" Width="80" Margin="0,10,0,0"
                    Click = "DelAll_Click" HorizontalAlignment="Center"/>
            <TextBlock x:Name = "textBlock" TextWrapping = "Wrap" 
                       Text = "Name" Width = "100"/>
            <TextBox x:Name = "textBox" TextWrapping = "Wrap" Width = "100"/>
            <TextBlock x:Name = "textBlock1" TextWrapping = "Wrap" 
                       Text = "Age" Width = "100"/>
            <TextBox x:Name = "textBox1" TextWrapping = "Wrap" Width = "100"/>
            <TextBlock x:Name = "textBlock2" TextWrapping = "Wrap" 
                       Width = "324" Margin="10"/>
        </StackPanel>
        
    </Grid>
</Page>
  逻辑:
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SQLite.Net;
using SQLite.Net.Platform.WinRT;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; //“空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 上有介绍 namespace Sqlite
{
/// <summary>
/// 可用于自身或导航至 Frame 内部的空白页。
/// </summary>
public sealed partial class MainPage : Page
{
string path = Path.Combine(ApplicationData.Current.LocalFolder.Path, "mydb.sqlite"); //建立数据库
SQLiteConnection conn; public MainPage()
{
this.InitializeComponent();
//建立数据库连接
conn = new SQLiteConnection(new SQLitePlatformWinRT(), path);
//建表
conn.CreateTable<DataTemple>(); //默认表名同范型参数
//以下等效上面的建表
//conn.CreateTable(typeof(DataTemple));
} //查询所有
private void Retrieve_Click(object sender, RoutedEventArgs e)
{
textBlock2.Text = "";
List<DataTemple> datalist = conn.Query<DataTemple>("select * from DataTemple"); /////////下面跟上面等效////////////////
//List<DataTemple> datalist = conn.Table<DataTemple>().ToList(); foreach (var item in datalist)
{
textBlock2.Text += item.Id + " " + item.Name + " " + item.Age + "\n";
}
} //添加一个记录
private void Add_Click(object sender, RoutedEventArgs e)
{
conn.Insert(new DataTemple() { Name = textBox.Text, Age = textBox1.Text });
} //删除一项
private void Del_Click(object sender, RoutedEventArgs e)
{
conn.Execute("delete from DataTemple where Name = ?", textBox.Text);
} //查询
private void Select_Click(object sender, RoutedEventArgs e)
{
textBlock2.Text = "";
List<DataTemple> datalist = conn.Query<DataTemple>("select * from DataTemple where Name = ?", textBox.Text); //////////////下面的也是查询////////////////////
//List<DataTemple> datalist = conn.Table<DataTemple>().Where(v => v.Name==name).ToList(); foreach (var item in datalist)
{
textBlock2.Text += item.Id + " " + item.Name + " " + item.Age + "\n";
}
} //删除全部记录
private void DelAll_Click(object sender, RoutedEventArgs e)
{
conn.DeleteAll<DataTemple>();
}
}
}

  界面:








                    https://github.com/oysteinkrog/SQLite.Net-PCL     (在test分支里面有示例代码)

最新文章

  1. C#文件或文件夹压缩和解压方法(通过ICSharpCode.SharpZipLib.dll)
  2. Android 第3方控件一览表
  3. Spring4.1新特性——Spring MVC增强
  4. paip.c3p0 nullpointexcept 配置文件根路径读取bug 解决
  5. bzoj 1856: [Scoi2010]字符串
  6. SQL SERVER -&gt;&gt; BCP导出数据到平面文件
  7. js基础第八天
  8. bzoj4443 SCOI2015 小凸玩矩阵 matrix
  9. 网站建设之Django搭建与配置
  10. WinFrom - DataGridView控件右键选中记录并弹出菜单
  11. java大牛list
  12. 堆排序—Java
  13. 微信小程序快捷键
  14. Java并发之AQS详解
  15. 在个人博客中优雅的使用Gitalk评论插件
  16. C#导出Excel表格方法
  17. Traceur
  18. Lodop控件NewPage();测试输出空白页
  19. Ceph相关
  20. java框架之SpringBoot(16)-分布式及整合Dubbo

热门文章

  1. 【b501】谁拿了最多的奖学金
  2. 【a803】营救
  3. Java反射xml数据类
  4. cordova-plugin-android-update安卓版本更新插件使用
  5. windows - Cygwin和MinGW有什么区别?(MinGW从Cygwin 1.3.3版本中分离出来)
  6. Oracle数据库案例整理-Oracle系统执行失败-sql_trace至TRUE导致Oracle在根文件夹中缺乏可用空间
  7. C#中的Lambda总结
  8. UML类图几种”关系“的总结
  9. numpy 辨异(四)—— np.repeat 与 np.tile
  10. 使用Apache Tiles3.x构建界面布局