从C ++的Windows商店应用使用SQLite,WinRT的 [英] Using sqlite-winrt from a C++ Windows Store app
问题描述
我试图使用的sqlite-的WinRT 从Windows应用商店的C ++应用程序。我想专门使用Windows运行时的包装在这个包,而不是href=\"http://visualstudiogallery.msdn.microsoft.com/23f6c55a-4909-4b1f-80b1-25792b11639e\" rel=\"nofollow\">正规的。我想看看codePLEX页上给出的C#示例:
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:
// 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 ++相当于是code的。这是我到目前为止有:
But I am unable to figure out the exact C++ equivalent of that code. This is what I have so far:
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循环完成。任何指针?
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?
推荐答案
通过的<一个最新版本href=\"http://blogs.msdn.com/b/vcblog/archive/2013/11/18/announcing-the-visual-c-compiler-november-2013-ctp.aspx\"相对=nofollow> Visual C ++编译2013年11月CTP ,并为可恢复的支持和等待附带此版本中,C ++ code现在看起来像一个更可读如下片段:
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));
}
这篇关于从C ++的Windows商店应用使用SQLite,WinRT的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!