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

2019-02-21 01:40发布

问题:

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?

回答1:

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:

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

                    });
}

where actionToExecute will the whatever you want to do in the loop.



回答2:

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:

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


回答3:

I think DatabasesCx is simpler 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());


回答4:

Or you can do it with like this:

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

                        }
                    }



            });
     });
});