如何从 C++/WinRT UWP 应用程序连接到 SQL 服务器?
How to connect to SQL server from C++/WinRT UWP app?
我正在尝试创建连接到 MySQL 数据库的 C++/WinRT UWP 应用程序,但实际上任何查询方式都很棒;我只需要以方便的方式存储和更新我的数据并通过 LAN 使用它。
了解 WinRT 我被重定向到 UWP 文档,在那里我找到了使用 System.Data.SQLite/SqlClient 或 MySQL.Data 但所有这些 return 的方法:
You are trying to install this package into a project that targets 'native,Version=v0.0', but the package does not contain any assembly references or content files that are compatible with that framework.
表示不支持WinRT ("native")。
我找到了 "SQLite for Windows Runtime (Windows 8.1)",但是当我尝试 install-Package SQLite.WinRT
时,我遇到了同样的错误。在 WinRT 中存储关系数据的首选方式是什么,或者我做错了什么?
过去几周,我一直在让 SQLite 在 C++/WinRT 应用程序中运行。 SQLite 内置于 windows sdk 中,但我没有很快发现这一点,而是选择了使用 vcpkg and put out by sqlite.org.
中提供的官方版本。
我能够相当轻松地让库工作,但我无法掌握 C 接口的窍门。 vcpkg 中也有一些很好的 C++ 包装器,所以我尝试了其中的两个并决定使用 sqlite_modern_cpp。它似乎并没有被积极地使用,但它只是一个接口并且与 sqlite 的 3.28 版本一起工作得很好。
要开始使用 sqlite 或 sqlite_modern_cpp 我使用基本相同的方法。
1.使用 vcpkg
安装其中之一
.\vcpkg.exe install sqlite-modern-cpp:x64-uwp
或
.\vcpkg.exe install sqlite3:x64-uwp
2。在 vcpkg
中启用用户范围的集成
.\vcpkg.exe integrate install
3。使用 C++/WinRT 空白应用模板创建新项目
4。更改为 x64 build
5.更改为 x64 后,您应该能够将以下内容添加到 pch.h
#include "sqlite_modern_cpp.h"
或
#include "sqlite3.h"
6.现在您可以创建一个 sqlite 数据库并添加一个 table
此示例使用 sqlite-modern-cpp 并被放入空白应用程序模板的点击处理程序中,只是为了有地方放置它。
void MainPage::ClickHandler(IInspectable const&, RoutedEventArgs const&)
{
myButton().Content(box_value(L"Clicked"));
// path for the temporary database file
hstring path{ ApplicationData::Current().LocalFolder().Path() + L"\temp.db" };
// store the file name for use later
std::string fname = to_string(path.c_str());
// open a connection to the database or create the file if it doesn't exist
sqlite::database db(fname); // default is READWRITE | CREATE
// enable foreign keys
db << "PRAGMA foreign_keys = ON;";
// create a new table, if needed
db << "CREATE TABLE IF NOT EXISTS division ("
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
" name TEXT NOT NULL,"
" distance INTEGER NOT NULL,"
" size INTEGER NOT NULL,"
" lanes INTEGER NOT NULL,"
" rank INTEGER NOT NULL,"
" progression TEXT NOT NULL,"
" UNIQUE (name, distance, size)"
");";
}
7. 运行 应用程序并单击按钮将生成数据库。
使用上述方法,将在应用程序本地文件夹中创建文件。
C:\用户\\AppData\Local\Packages\\LocalState\temp.db
我正在尝试创建连接到 MySQL 数据库的 C++/WinRT UWP 应用程序,但实际上任何查询方式都很棒;我只需要以方便的方式存储和更新我的数据并通过 LAN 使用它。
了解 WinRT 我被重定向到 UWP 文档,在那里我找到了使用 System.Data.SQLite/SqlClient 或 MySQL.Data 但所有这些 return 的方法:
You are trying to install this package into a project that targets 'native,Version=v0.0', but the package does not contain any assembly references or content files that are compatible with that framework.
表示不支持WinRT ("native")。
我找到了 "SQLite for Windows Runtime (Windows 8.1)",但是当我尝试 install-Package SQLite.WinRT
时,我遇到了同样的错误。在 WinRT 中存储关系数据的首选方式是什么,或者我做错了什么?
过去几周,我一直在让 SQLite 在 C++/WinRT 应用程序中运行。 SQLite 内置于 windows sdk 中,但我没有很快发现这一点,而是选择了使用 vcpkg and put out by sqlite.org.
中提供的官方版本。我能够相当轻松地让库工作,但我无法掌握 C 接口的窍门。 vcpkg 中也有一些很好的 C++ 包装器,所以我尝试了其中的两个并决定使用 sqlite_modern_cpp。它似乎并没有被积极地使用,但它只是一个接口并且与 sqlite 的 3.28 版本一起工作得很好。
要开始使用 sqlite 或 sqlite_modern_cpp 我使用基本相同的方法。
1.使用 vcpkg
安装其中之一.\vcpkg.exe install sqlite-modern-cpp:x64-uwp
或
.\vcpkg.exe install sqlite3:x64-uwp
2。在 vcpkg
中启用用户范围的集成.\vcpkg.exe integrate install
3。使用 C++/WinRT 空白应用模板创建新项目
4。更改为 x64 build
5.更改为 x64 后,您应该能够将以下内容添加到 pch.h
#include "sqlite_modern_cpp.h"
或
#include "sqlite3.h"
6.现在您可以创建一个 sqlite 数据库并添加一个 table
此示例使用 sqlite-modern-cpp 并被放入空白应用程序模板的点击处理程序中,只是为了有地方放置它。
void MainPage::ClickHandler(IInspectable const&, RoutedEventArgs const&)
{
myButton().Content(box_value(L"Clicked"));
// path for the temporary database file
hstring path{ ApplicationData::Current().LocalFolder().Path() + L"\temp.db" };
// store the file name for use later
std::string fname = to_string(path.c_str());
// open a connection to the database or create the file if it doesn't exist
sqlite::database db(fname); // default is READWRITE | CREATE
// enable foreign keys
db << "PRAGMA foreign_keys = ON;";
// create a new table, if needed
db << "CREATE TABLE IF NOT EXISTS division ("
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
" name TEXT NOT NULL,"
" distance INTEGER NOT NULL,"
" size INTEGER NOT NULL,"
" lanes INTEGER NOT NULL,"
" rank INTEGER NOT NULL,"
" progression TEXT NOT NULL,"
" UNIQUE (name, distance, size)"
");";
}
7. 运行 应用程序并单击按钮将生成数据库。
使用上述方法,将在应用程序本地文件夹中创建文件。 C:\用户\\AppData\Local\Packages\\LocalState\temp.db