gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:07:40 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com