Sqlite data reader for Windows phone 8.1

 ̄綄美尐妖づ 提交于 2019-12-11 09:39:33

问题


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).

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

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

where T is unknown?

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


回答1:


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).

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



来源:https://stackoverflow.com/questions/27743397/sqlite-data-reader-for-windows-phone-8-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!