How to connect to SQL server from C++/WinRT UWP app?
我正在尝试创建连接到MySQL数据库的C ++ / WinRT UWP应用程序,但实际上任何查询任何内容的方式都将是很棒的; 我只需要以一种方便的方式存储和更新我的数据,并通过LAN使用它。
了解WinRT后,我被重定向到UWP文档,在那里我找到了使用System.Data.SQLite / SqlClient或MySQL.Data的方法,但所有这些方法均返回以下内容:
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("本机")。
我已经找到"用于Windows运行时的SQLite(Windows 8.1)",但是当我尝试执行
在过去的几周中,我一直在让SQLite在C ++ / WinRT应用程序中工作。 SQLite内置在Windows sdk中,但我没有足够快地发现它,因此拒绝使用vlite中提供的官方版本,并由sqlite.org发布。
我可以很轻松地使库正常工作,但无法解决C接口的问题。 vcpkg中也提供了一些不错的C ++包装器,因此我尝试了其中的2个并决定使用sqlite_modern_cpp。它似乎没有被积极地研究,但它只是一个界面,并且可以与sqlite 3.28版本一起很好地工作。
要开始使用sqlite或sqlite_modern_cpp,我使用基本相同的方法。
1.使用vcpkg安装其中之一
1 | .\\vcpkg.exe install sqlite-modern-cpp:x64-uwp |
要么
1 | .\\vcpkg.exe install sqlite3:x64-uwp |
2.在vcpkg中启用用户范围的集成
1 | .\\vcpkg.exe integrate install |
3.使用C ++ / WinRT空白应用程序模板创建一个新项目
4.更改为x64版本
5.更改为x64后,应该可以将以下内容添加到pch.h中
1 | #include"sqlite_modern_cpp.h" |
要么
1 | #include"sqlite3.h" |
6.现在,您可以创建一个sqlite数据库并添加一个表
本示例使用sqlite-modern-cpp,并将其放置在空白应用程序模板的点击处理程序中只是为了放置它。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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:\ Users \\ AppData \ Local \ Packages \\ LocalState \ temp.db