从C++ Windows 应用商店应用程序中使用 sqlite-winrt
Using sqlite-winrt from a C++ Windows Store app
我正在尝试从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) {
}
}
});
});
});
相关文章:
- 尝试从 XamlApplication 编译 C++/WinRT 空白应用程序时,我收到未解析的外部符号 winrt_make_*
- 在 Winrt/C++ 中使用应用服务
- 是否可以从控制台应用程序使用 C++/WinRT 创建窗口?
- WinRT 应用调用 win32 c++ dll 发送参数
- C++应用程序缺少 winrt::Windows::Storage::Streams::D ataWriter
- 我可以用C++/WinRT编写一个DLL,供windows桌面应用程序(而不是UWP)或Unity插件使用吗
- 如何从 c++ winrt UWP 应用中的代码将文本添加到富文本块
- 如何将SQLite c文件(合并)与cpp应用程序链接?
- 是否可以使用C /WinRT创建Windows Service应用程序
- 您如何使用Winrt创建经典的Win32应用程序窗口
- QT SQLITE登录应用程序
- 如何使用现有的 Sqlite 数据库部署 Qt 应用程序
- 使用 Visual C++ 创建的 WinRT DLL 不会在 Windows 应用商店应用中加载
- 运行C 应用程序后出现SQLite控制台
- 带有API的WinRT应用程序,该应用程序使用共享的MFC dll
- 从C++ Windows 应用商店应用程序中使用 sqlite-winrt
- 在Windows8中编写C#/XAML与C++/XAML WinRT应用程序的优缺点是什么
- 如果WinRT应用程序在文件复制过程中终止,会发生什么
- 我需要一个Windows 8 WinRT应用程序来与用C编写的独立应用程序通信
- 在qt中开发的我的应用程序中与Sqlite的应用程序连接