I am trying to use sqlite-winrt from a Windows Store C++ app. I want to specifically use the Windows Runtime wrapper in this package as opposed to the regular C API from this package. I am trying to look at the C# example given on the codeplex page:
C# code
// 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));
But I am unable to figure out the exact C++ equivalent of that code. This is what I have so far:
C++ code
auto installLoc = Windows::ApplicationModel::Package::Current->InstalledLocation;
task<Windows::Storage::StorageFile^>(installLoc->GetFileAsync("cities.db")).then([](Windows::Storage::StorageFile^ file){
auto db = ref new SQLiteWinRT::Database(file);
task<void>(db->OpenAsync(SQLiteWinRT::SqliteOpenMode::OpenRead)).then([db](){
task<SQLiteWinRT::Statement^>(db->PrepareStatementAsync("SELECT rowid, CityName FROM Cities;")).then([](SQLiteWinRT::Statement^ stmt){
// Don't know how to simulate the while loop
//task<bool>(stmt->StepAsync()).then([](bool ret){
//});
});
});
});
I am not quite able to figure out how to simulate the behavior of iteration as done using the while loop in C#. Any pointers?
I think DatabasesCx is simpler http://www.almanacsoft.com/databasescx
Or you can do it with like this:
Not sure if that's the easiest way but one way would be to transform the loop into a recursive call. For example something like this:
where actionToExecute will the whatever you want to do in the loop.
With the latest release of the Visual C++ Compiler November 2013 CTP, and the support for resumable and await that comes with this release, the C++ code now looks like a much more readable below snippet: