c++ - 从 C++ Windows 应用商店应用程序使用 sqlite-winrt

标签 c++ sqlite asynchronous windows-runtime async-await

我正在尝试使用 sqlite-winrt来自 Windows Store C++ 应用程序。我想在此包中专门使用 Windows 运行时包装器,而不是 regular C API from this package .我正在尝试查看 codeplex 页面上给出的 C# 示例:


C#代码

  // 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));

但我无法找出该代码的确切 C++ 等价物。这是我目前所拥有的:


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){
                           // Don't know how to simulate the while loop
                           //task<bool>(stmt->StepAsync()).then([](bool ret){
                           //});
                     });
              });
       });

我不太明白如何使用 C# 中的 while 循环来模拟迭代行为。有什么指点吗?

最佳答案

不确定这是否是最简单的方法,但一种方法是将循环转换为递归调用。例如这样的事情:

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([]{});

                    });
}

其中 actionToExecute 将是您想在循环中执行的任何操作。

关于c++ - 从 C++ Windows 应用商店应用程序使用 sqlite-winrt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19309508/

相关文章:

c++ - C++ 中的 typeid 运算符

android - 为 "one row"数据使用 sharedPreferences 或数据库

reactjs - Knex 与 Electron 和 sqlite 的迁移?

javascript - 如何防止 ASP.net PageMethod 调用阻止其他 javascript 函数调用

ios - SKProductsRequestDelegate 方法未在类内调用

c++ - 带有制表符的 strcat 操作不起作用

c++ - 为什么一个语句调用复制构造函数,而另一个语句调用复制赋值运算符?

c++ -/usr/include/boost/filesystem/path.hpp:307: 未定义对`boost::filesystem::path::operator 的引用

python - Django 和 Github 上的数据库管理

java - 在测试 AsyncRestTemplate 时防止期望已经声明异常