0

我正在尝试使用Windows Store C++ 应用程序中的sqlite-winrt。我想专门使用这个包中的 Windows 运行时包装器,而不是这个包中的常规 C API。我正在尝试查看 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 循环那样模拟迭代的行为。任何指针?

4

4 回答 4

1

随着Visual C++ 编译器 2013 年 11 月 CTP的最新版本,以及此版本附带的对可恢复和等待的支持,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));
}
于 2013-12-17T04:34:06.300 回答
1

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

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 将在循环中执行您想要执行的任何操作。

于 2013-10-13T01:14:45.360 回答
0

我认为 DatabasesCx 更简单http://www.almanacsoft.com/databasescx

        String^ pathName = Windows::Storage::ApplicationData::Current->LocalFolder->Path + "\\cities.db";
        SQLiteCx^ mySql = ref new DatabasesCx::SQLiteCx(pathName);
        auto rowsOut = ref new Vector<RowCx^>();
        Concurrency::create_task(mySql->GetAsync(L"SELECT rowid, CityName FROM Cities", rowsOut))
       .then([this, rowsOut]()
       {  
          itemsViewSource->Source = rowsOut; // Data Binding to control
       }, task_continuation_context::use_current());
于 2014-03-15T06:03:47.587 回答
0

或者你可以这样做:

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

                        }
                    }



            });
     });
});
于 2017-05-25T21:29:38.970 回答