UWP SQLite的使用
1.准备工作
1.首先我们要给项目添加“SQLite for Universal Windows Platform”扩展
点击菜单栏的“工具”-“扩展和更新”,然后在“联机”中搜索
2.安装扩展后我们再添加“SQLite.Net-PCL ”的Nuget程序包
在解决方案管理器中右击项目的“引用”,打开“管理Nuget程序包管理”,在“浏览”中搜索并安装
现在已经准备完成了,可以开始使用了。
2.使用
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变量):
List<DataTemple> query = conn.Table<DataTemple>().Where(v => v.Name == name).ToList();
List<DataTemple> query = conn.Query<DataTemple>("select * from DataTemple where Name = ?", name);
查询全部:
List<DataTemple> query = conn.Table<DataTemple>().ToList();
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>();
}
}
}
UWP SQLite的使用的更多相关文章
- UWP开发之ORM实践:如何使用Entity Framework Core做SQLite数据持久层?
选择SQLite的理由 在做UWP开发的时候我们首选的本地数据库一般都是Sqlite,我以前也不知道为啥?后来仔细研究了一下也是有原因的: 1,微软做的UWP应用大部分也是用Sqlite.或者说是微软 ...
- Win10 UWP 开发系列:支持异步的SQLite
上篇文章已经实现了在UWP中使用SQLite作为本地存储,作为移动端的程序,及时响应用户的操作是提高用户体验的重要途径,因此UWP的很多api都是异步的.那么如何使SQLite支持异步呢? 参考SQL ...
- Win10 UWP 开发系列:使用SQLite
在App开发过程中,肯定需要有一些数据要存储在本地,简单的配置可以序列化后存成文件,比如LocalSettings的方式,或保存在独立存储中.但如果数据多的话,还是需要本地数据库的支持.在UWP开发中 ...
- UWP开发随笔——使用SQLite数据库
摘要 大多数的app都需要数据存储,在数据存储这方面,强大的windows把app数据分为两种:settings和files,并提供了十分简洁的api,让开发者能够轻松使用.但是在有些场景下,app的 ...
- [UWP小白日记-11]在UWP中使用Entity Framework Core(Entity Framework 7)操作SQLite数据库(一)
前言 本文中,您将创建一个通用应用程序(UWP),使用Entity Framework Core(Entity Framework 7)框架在SQLite数据库上执行基本的数据访问. 准备: Enti ...
- [UWP小白日记-2]SQLite数据库DOME
数据库说简单点就是增删改查,但是对新手来说也是要爆肝的.作为一个新手爆肝无数次啊, 血的教训啊现在UWP的教程又少,说多了都是泪.留下来免得以后又爆肝.还有:一定要写注释!一定要写注释!一定要写注释! ...
- UWP: 在 UWP 中使用 Entity Framework Core 操作 SQLite 数据库
在应用中使用 SQLite 数据库来存储数据是相当常见的.在 UWP 平台中要使用 SQLite,一般会使用 SQLite for Universal Windows Platform 和 SQLit ...
- UWP开发-在UWP中使用sqlite
原文:UWP开发-在UWP中使用sqlite sqlite是一种轻量级的数据库,对于一些资源紧张又需要数据库的开发非常好用. SQLite 是一个开源的无服务器嵌入式数据库. 这些年来,它已作为面向存 ...
- UWP使用Microsoft.Data.Sqlite的记录
我在UWP中使用SQLite数据库时,并没有使用网上的SQLite for Universal App Platform方案,而使用了Microsoft和SQLite社区一起维护的Microsoft. ...
随机推荐
- [Typescript 2] Nullable Types - Avoiding null and undefined Bugs
For example you have a TS app: enum PaylerPosition { Guard, Forward, Center } interface Player { nam ...
- hbase 2.0.2 put和delete的一些坑
测试的inbox表为多版本表,封装的scanTable已设置查询全部版本,以下的测试基于hbase2.0.2 一.put(针对相同的rowkey) 测试1.使用方法链的形式对同一个put添加数据到不同 ...
- 【codeforces 785B】Anton and Classes
[题目链接]:http://codeforces.com/contest/785/problem/B [题意] 给你两个时间各自能够在哪些时间段去完成; 让你选择两个时间段来完成这两件事情; 要求两段 ...
- 【25.00%】【codeforces 584E】Anton and Ira
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- C#实现拼图游戏
C#实现<拼图游戏> (下) 原理篇 前言:在 http://www.cnblogs.com/labixiaohei/p/6698887.html 程序设计 之 C#实现<拼图游 ...
- Matlab Tricks(三十) —— 任意区间的均匀分布
matlab 的内置函数 rand返回的是 0-1 区间上的均匀分布,rand的参数多是用于设置返回的矩阵的维度大小. 如果要得到 (a, b) 区间上的均匀分布,只需对其做简单的线性变换即可: a+ ...
- 在web应用程序中在网页中对Web.config文件进行数据连接配置
using System; using System.Collections.Generic; using System.Configuration; using System.Web.Configu ...
- sql数据库恢复神器--Log Explorer 4.2使用教程
对于程序员来说,世界最悲催的事情是什么?——就是手贱,把数据库的数据给删掉了,更悲催的是木有任何数据库备份 感谢万能的度娘,感谢无私奉献的网友们,最感谢强大的LogExplorer工具 . 使用Lo ...
- R 语言的学习(一)
1. 基本 "hello world!" > "hello world!" [1] "hello world!" 这在 R 中并不是一 ...
- DDD实战12 值对象不创建表,而是直接作为实体中的字段
这里的值对象如下风格: namespace Order.Domain.PocoModels { //订单地址 //虽然是值对象 但是不继承ValueObject //因为继承ValueObject会有 ...