如何在 WinRT DLL 中使用 SQLite?
我正在尝试开发一个使用 SQLite 编写数据库的 WinRT DLL。 但似乎SQLite源代码中的一些win32 API不受metro支持,例如LoadLibraryW
、GetTempPathA
。
有没有办法编译 SQLite 源代码或将 SQLite 与 WinRT DLL 一起使用?
I am trying to develop a WinRT DLL which uses SQLite to write database.
But it seems like some win32 APIs in SQLite source code are not supported by metro, such as, LoadLibraryW
, GetTempPathA
.
Is there any way to compile SQLite source code or use SQLite with WinRT DLL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
好吧,您始终可以静态链接 sqlite3 并通过 sqlite3_vfs 定义用于访问文件等的新函数。
Well you could always link sqlite3 statically and define new functions for accessing files etc via sqlite3_vfs.
在 VS2012 中,现在有一个名为 SQLite for Windows Runtime 的扩展。您可以通过 Visual Studio 下载并安装它(需要重新启动 IDE)。然后,转到您的 WinRT 项目,添加引用,在“Windows”下选择“扩展”,您应该会看到它。
In VS2012 there is an extension now called SQLite for Windows Runtime. You can download and install this via Visual Studio (requires a restart of the IDE). Then, go to your WinRT project, Add a Reference, under "Windows" choose "Extensions" and you should see it.
SQLite 现在有一个 winrt 分支,仅使用受支持的 API。最重要的是,我们实现了 SQLite3-WinRT,这是一个 WinRT 组件,允许在任何WinRT 语言。
There's a winrt branch of SQLite now that only uses supported API. On top of that, we implemented SQLite3-WinRT, a WinRT component that allows using SQLite in any of the WinRT languages.
sqlite3.c
重命名为sqlite3.cpp
LoadLibrary
替换为LoadPackagesLibrary
sqlite3.c
tosqlite3.cpp
LoadLibrary
withLoadPackagedLibrary
来自 SQLite 站点
Tim Heuer 提供了演练在他的博客上使用 SQLite 构建 Metro 应用
From the SQLite site
Tim Heuer provides a walk-through of building a metro app using SQLite on his blog
让我添加一些关于使用 sqlite/winrt 的注释,这可能会为您省去一些麻烦:
Winrt 允许您仅写入特定文件夹 (c:\users\\My Documents\)。你必须把你的数据库放在这里。 (在托管环境中很简单。)
Sqlite 使用临时文件的时间。 (需要瞬态索引、真空等的复杂查询)这些文件也必须在应用程序文件夹中创建,但 sqlite 不会执行此操作,除非您使用 temp_store_directory pragma 设置它。如果您不这样做,您可能会收到随机的用户错误报告。
请注意,上述编译指示已被正式弃用。忽略这一点。本机编码人员可能会尝试使用全局变量 sqlite3_temp_directory (鼓励方式),但当前的二进制版本 (dll) 不发布此变量。 (您可以自己执行此操作,但随后更改 sqlite 源并使用 _declspec(dllexport) 属性;def 文件不起作用。)
不要过多依赖 sqlite 文件操作。实施效果不是特别好。例如,即使您没有写权限,写访问测试也会成功。
除此之外,winrt 似乎没有任何问题。更高级的用户可能会提供他们自己的(更好的)winrt 驱动程序。这并不太难...
Let me add some remarks on using sqlite/winrt that may save you some headaches:
Winrt allows you to write only to specific folder (c:\users\<user>\My documents\<app>). You have to put your DB here. (Trivial in managed environment.)
Time from time Sqlite uses temp files. (Complex queries that need transient indices, vacuum etc.) These files also must be created in the app folder, but sqlite won't do this unless you set it with temp_store_directory pragma. If you don't do this, you may get random user bug reports.
Note that above pragma is officially deprecated. Ignore this. Native coders might be tempted to use global variable sqlite3_temp_directory instead (encouraged way), but the current binary release (dll) does not publish this variable. (You can do it yourself, but then change sqlite sources and use _declspec(dllexport) attribute; def file does not work.)
Don't rely too much on sqlite file operations. The implementation is not particularly good. For example testing of write access will succeed even if you don't have write permissions.
Apart from this there seem to be no problems with winrt. More advanced users might supply their own (better) winrt driver. It is not too difficult...