如何从数据库中获取数据? Winrt 上的 SQLite
How to get data from database? SQLite on Winrt
我在我的项目文件夹中得到了随时可用的 .db 数据库,其中包含字段 "city"、"area" 并添加了数据。
那么如何从城市 == 某些值的数据库中获取信息?
我使用 C#、sqlite-net 和 SQLite。
假设您有如下单个 table 数据库,如下所示:
- 将数据库“.db”文件添加到您的项目中,我选择将其放入 数据文件夹 以保持项目结构,
- 将数据库文件的构建操作设置为内容,以便在编译时将其复制到AppX文件,
- 现在,需要将“.db”文件复制到Local(或Roaming)根据您的需要和数据库的大小,将以下方法添加到 App.xaml.cs 文件
private async Task CopyDatabase()
{
bool isDatabaseExisting = false;
try
{
var s = ApplicationData.Current.LocalFolder.Path;
StorageFile storageFile
= await ApplicationData.Current.LocalFolder.GetFileAsync("DataBase.db");
isDatabaseExisting = true;
}
catch
{
isDatabaseExisting = false;
}
if (!isDatabaseExisting)
{
StorageFile databaseFile = await
Package.Current.InstalledLocation.GetFileAsync(@"Data\DataBase.db");
await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);
}
}
以上代码会将数据库从 InstalledLocation (Appx),
复制到 Local 文件夹
在OnLaunched
方法中调用这个方法:
为了让您在数据库上执行 Sql 查询,有几种方法,其中之一是使用 SQLitePCL library, so add it to your project using Nuget:
Install-Package SQLitePCL
添加对 SQLite 的引用,用于 Windows 运行时 (Windows 8.1) 使用 Vs extension
添加数据模型:为数据库中的每个 table 创建一个 class 将保持相同的结构,这里我们只有一个 table ' Data
':
public class Data
{
public long Id { get; set; }
public String City { get; set; }
public String Area { get; set; }
}
添加一个 class 来保存所有数据的交互逻辑,并使用 Sqlite.netPcl
库添加所有需要的数据库操作:
public class DataService
{
private readonly SQLiteConnection _connection;
public DataService()
{
_connection = new SQLiteConnection("DataBase.db");
}
public async Task<List<Data>> GetAllCities()
{
var cities = new List<Data>();
using (var statement = _connection.Prepare("SELECT * FROM Data"))
{
while (statement.Step() == SQLiteResult.ROW)
{
cities.Add(new Data()
{
Id = (long)statement[0],
City = (string)statement[1],
Area = (string)statement[2]
});
}
}
return cities;
}
}
这里的方法不需要async
,但是在高级场景下你可以考虑,
最后要获取数据只需调用GetAllCities
方法:
public async void MainPage_OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
var dataService=new DataService();
ListV.ItemsSource = await dataService.GetAllCities();
}
Xaml :
<ListView x:Name="ListV" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding City}"/>
<TextBlock Text="{Binding Area}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我在我的项目文件夹中得到了随时可用的 .db 数据库,其中包含字段 "city"、"area" 并添加了数据。
那么如何从城市 == 某些值的数据库中获取信息?
我使用 C#、sqlite-net 和 SQLite。
假设您有如下单个 table 数据库,如下所示:
- 将数据库“.db”文件添加到您的项目中,我选择将其放入 数据文件夹 以保持项目结构,
- 将数据库文件的构建操作设置为内容,以便在编译时将其复制到AppX文件,
private async Task CopyDatabase()
{
bool isDatabaseExisting = false;
try
{
var s = ApplicationData.Current.LocalFolder.Path;
StorageFile storageFile
= await ApplicationData.Current.LocalFolder.GetFileAsync("DataBase.db");
isDatabaseExisting = true;
}
catch
{
isDatabaseExisting = false;
}
if (!isDatabaseExisting)
{
StorageFile databaseFile = await
Package.Current.InstalledLocation.GetFileAsync(@"Data\DataBase.db");
await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);
}
}
以上代码会将数据库从 InstalledLocation (Appx),
复制到 Local 文件夹在
OnLaunched
方法中调用这个方法:为了让您在数据库上执行 Sql 查询,有几种方法,其中之一是使用 SQLitePCL library, so add it to your project using Nuget:
Install-Package SQLitePCL
添加对 SQLite 的引用,用于 Windows 运行时 (Windows 8.1) 使用 Vs extension
添加数据模型:为数据库中的每个 table 创建一个 class 将保持相同的结构,这里我们只有一个 table '
Data
':public class Data { public long Id { get; set; } public String City { get; set; } public String Area { get; set; } }
添加一个 class 来保存所有数据的交互逻辑,并使用
Sqlite.netPcl
库添加所有需要的数据库操作:public class DataService { private readonly SQLiteConnection _connection; public DataService() { _connection = new SQLiteConnection("DataBase.db"); } public async Task<List<Data>> GetAllCities() { var cities = new List<Data>(); using (var statement = _connection.Prepare("SELECT * FROM Data")) { while (statement.Step() == SQLiteResult.ROW) { cities.Add(new Data() { Id = (long)statement[0], City = (string)statement[1], Area = (string)statement[2] }); } } return cities; }
}
这里的方法不需要async
,但是在高级场景下你可以考虑,
最后要获取数据只需调用
GetAllCities
方法:public async void MainPage_OnLoaded(object sender, RoutedEventArgs routedEventArgs) { var dataService=new DataService(); ListV.ItemsSource = await dataService.GetAllCities(); }
Xaml :
<ListView x:Name="ListV" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding City}"/>
<TextBlock Text="{Binding Area}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>