适用于 Windows 手机 8.1 的 Sqlite 数据阅读器 [英] Sqlite data reader for Windows phone 8.1

查看:36
本文介绍了适用于 Windows 手机 8.1 的 Sqlite 数据阅读器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前适用于 windows phone 8.1 的 SQLite 没有附带 SQLite 数据读取器,但我需要在不知道类型的情况下从 SQLite DB 读取数据,这是因为表数据和架构可以从外部更改(在我的应用程序之外).

Currently SQLite for windows phone 8.1 doesn't come with SQLite Data reader, But I need to read data from SQLite DB without knowing the Type in advance , this is because the Table data and schema can change externally (Outside of my application).

那么有什么办法可以读取下面的SQLite表数据

So is there any way to read the SQLite table data as below

var connection = new SQLiteConnection(dbName);
connection.Query<T>("Select * from Table") 

其中 T 未知?

或者,是否可以从 connection.GetTableInfo("Table name") 获取的 Column 列表动态创建 T?

Alternatively , is it possible to create T dynamically from the Column list obtained from the connection.GetTableInfo("Table name")?

推荐答案

您可以使用 Andy Wigley 的 SQLiteWinRT 包装器,它支持普通的非类型 SQL (https://sqlwinrt.codeplex.com/).

You can use Andy Wigley's SQLiteWinRT wrapper which supports plain non-typed SQL (https://sqlwinrt.codeplex.com/).

您可以使用普通的旧语句:

You can either use plain old statements:

// Get the file from the install location  
var file = await Package.Current.InstalledLocation.GetFileAsync("cities.db");  

// Create a new SQLite instance for the file 
var db = new Database(file);  

// Open the database asynchronously
await db.OpenAsync(SqliteOpenMode.OpenRead);

// Prepare a SQL statement to be executed
var statement = awaitdb.PrepareStatementAsync(
  "SELECT rowid, CityName FROM Cities;"); 

// Loop through all the results and add to the collection
while (awaitstatement.StepAsync())
   items.Add(statement.GetIntAt(0) + ": "+ statement.GetTextAt(1));

或者你可以使用准备好的语句(这当然更好,因为它提供了更多的模块化):

Or you can use prepared statements (which is certainly better as it provides more modularity):

// Prepare a SQL statement to be executed with a parameter
var statement = await db.PrepareStatementAsync(
  "SELECT rowid, CityName FROM Cities WHERE CityName LIKE ?;");

// Bind the parameter value to the statement
statement.BindTextParameterAt(1, "c%");

// Loop through all the results and add to the collection
// Same as above

如您所见,查询返回可用于构建对象的简单字符串.或者您可以直接使用这些对象(您提到您不一定了解底层对象).

As you can see the queries return simple strings which you can use to construct your objects. Or you can work directly with those (you mentioned that you don't necessarily know about the underlying objects).

这是另一个可以帮助您入门的教程:http://blogs.windows.com/buildingapps/2013/05/30/sqlite-winrt-wrapper-for-windows-phone/

Here is another tutorial that should get you started: http://blogs.windows.com/buildingapps/2013/05/30/sqlite-winrt-wrapper-for-windows-phone/

这篇关于适用于 Windows 手机 8.1 的 Sqlite 数据阅读器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆