从C++ Windows存储应用程序中使用SQLite WINRT 我试图从Windows商店C++应用程序中使用。我想在这个包中专门使用Windows运行时包装,而不是。我试图查看codeplex页面上给出的C示例: C代码 但是我无法计算出那个代码的C++等价。这就是我到目前为止所做的: C++代码
我不太清楚如何使用C中的while循环模拟迭代行为。有指针吗?不确定这是否是最简单的方法,但一种方法是将循环转换为递归调用。例如,类似这样的事情:从C++ Windows存储应用程序中使用SQLite WINRT 我试图从Windows商店C++应用程序中使用。我想在这个包中专门使用Windows运行时包装,而不是。我试图查看codeplex页面上给出的C示例: C代码 但是我无法计算出那个代码的C++等价。这就是我到目前为止所做的: C++代码,c++,sqlite,asynchronous,windows-runtime,async-await,C++,Sqlite,Asynchronous,Windows Runtime,Async Await,我不太清楚如何使用C中的while循环模拟迭代行为。有指针吗?不确定这是否是最简单的方法,但一种方法是将循环转换为递归调用。例如,类似这样的事情: task<void> stepInfoRecursive(std::function<void()> actionToExecute,SQLiteWinRT::Statement^ stmt) { return task<bool>(stmt->StepAsync()).then([actionToE
task<void> stepInfoRecursive(std::function<void()> actionToExecute,SQLiteWinRT::Statement^ stmt)
{
return task<bool>(stmt->StepAsync()).then([actionToExecute,stmt](bool ret){
actionToExecute();
if (ret){
return stepInfoRecursive(actionToExecute,stmt);
}
return create_task([]{});
});
}
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){
while (bool res = task<bool>(stmt->StepAsync()).get()) {
if (res == true) {
}
}
});
});
});
在Actuto to Excice中,您将在循环中做任何事情。
< P>随着最新版本的发布,以及对可恢复和等待的支持,在这个版本中,C++代码现在看起来更容易读到下面的代码段:void MainPage::DoStuff() __resumable
{
auto items = ref new Vector<String^>();
auto file = __await Package::Current->InstalledLocation->GetFileAsync("cities.db");
auto db = ref new SQLiteWinRT::Database(file);
__await db->OpenAsync(SQLiteWinRT::SqliteOpenMode::OpenRead);
auto stmt = __await db->PrepareStatementAsync("SELECT rowid, CityName FROM Cities;");
while (__await stmt->StepAsync())
items->Append(stmt->GetIntAt(0) + ": " + stmt->GetTextAt(1));
}
我认为DatabasesCx更简单
或者你可以这样做:
task<void> stepInfoRecursive(std::function<void()> actionToExecute,SQLiteWinRT::Statement^ stmt)
{
return task<bool>(stmt->StepAsync()).then([actionToExecute,stmt](bool ret){
actionToExecute();
if (ret){
return stepInfoRecursive(actionToExecute,stmt);
}
return create_task([]{});
});
}
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){
while (bool res = task<bool>(stmt->StepAsync()).get()) {
if (res == true) {
}
}
});
});
});
有关C++中异步的更多例子,请看一下这个博客文章:
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){
while (bool res = task<bool>(stmt->StepAsync()).get()) {
if (res == true) {
}
}
});
});
});