问题
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