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

Using sqlite-winrt from a C++ Windows Store app

本文关键字:sqlite-winrt 应用程序 C++ Windows 应用      更新时间:2023-10-16

我正在尝试从Windows Store C++应用程序中使用sqlite-winrt。 我想在此包中专门使用 Windows 运行时包装器,而不是此包中的常规 C API。 我正在尝试查看代码复合页面上给出的 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 将在循环中执行您想要执行的任何操作。

随着 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));
}

我认为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());

或者你可以像这样做:

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

            });
     });
});