开发者

How to use SQLite within a WinRT DLL?

I am trying to develop a WinRT DLL which uses SQLite to write database. But i开发者_如何学Ct 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?


Well you could always link sqlite3 statically and define new functions for accessing files etc via sqlite3_vfs.


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.

How to use SQLite within a WinRT DLL?


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.


  1. rename sqlite3.c to sqlite3.cpp
  2. replace LoadLibrary with LoadPackagedLibrary
  3. Fix lots of syntax errors.


From the SQLite site

SQLite version 3.7.13 adds support for WinRT and metro style applications for Microsoft Windows 8. The 3.7.13 release is coming sooner than is usual after the previous release in order to get this new capability into the hands of developers. To use SQLite in a metro style application, compile with the -DSQLITE_OS_WINRT flag. Because of the increased application security and safety requirements of WinRT, all database filenames should be full pathnames. Note that SQLite is not capable of accessing databases outside the installation directory and application data directory. This restriction is another security and safety feature of WinRT. Apart from these restrictions, SQLite should work exactly the same on WinRT as it does on every other system.

Tim Heuer provides a walk-through of building a metro app using SQLite on his blog


Let me add some remarks on using sqlite/winrt that may save you some headaches:

  1. 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.)

  2. 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.

  3. 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.)

  4. 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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜