UWP SQLite的使用
2024-08-25 21:01:37
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分支里面有示例代码)
最新文章
- C#文件或文件夹压缩和解压方法(通过ICSharpCode.SharpZipLib.dll)
- Android 第3方控件一览表
- Spring4.1新特性——Spring MVC增强
- paip.c3p0 nullpointexcept 配置文件根路径读取bug 解决
- bzoj 1856: [Scoi2010]字符串
- SQL SERVER ->;>; BCP导出数据到平面文件
- js基础第八天
- bzoj4443 SCOI2015 小凸玩矩阵 matrix
- 网站建设之Django搭建与配置
- WinFrom - DataGridView控件右键选中记录并弹出菜单
- java大牛list
- 堆排序—Java
- 微信小程序快捷键
- Java并发之AQS详解
- 在个人博客中优雅的使用Gitalk评论插件
- C#导出Excel表格方法
- Traceur
- Lodop控件NewPage();测试输出空白页
- Ceph相关
- java框架之SpringBoot(16)-分布式及整合Dubbo
热门文章
- 【b501】谁拿了最多的奖学金
- 【a803】营救
- Java反射xml数据类
- cordova-plugin-android-update安卓版本更新插件使用
- windows - Cygwin和MinGW有什么区别?(MinGW从Cygwin 1.3.3版本中分离出来)
- Oracle数据库案例整理-Oracle系统执行失败-sql_trace至TRUE导致Oracle在根文件夹中缺乏可用空间
- C#中的Lambda总结
- UML类图几种”关系“的总结
- numpy 辨异(四)—— np.repeat 与 np.tile
- 使用Apache Tiles3.x构建界面布局