Using SQLite with WinRT

2019-07-07 00:03发布

问题:

I am developing a metro ui application and I would like to use SQLite for some internal data instead of JET in order to take advantage of some already-written code.

Howerver when I try to use sqlite3_open for opening a data base, it does not work. I get an error saying a cannot open the data base.

I believe some APIs used by the SQLite cannot be used on metro style application.

Can someone help me on this? At least say me how to identify what apis should be ported?

回答1:

This code should work:

auto localAppDataParh = Windows::Storage::ApplicationData::Current->LocalFolder->Path;
std::wstring path( localAppDataParh->Data() );
path += L"\\sample.db";
sqlite3* db;
int rc = sqlite3_open16( path.c_str(), &db);

I believe some APIs used by the SQLite cannot be used on metro style application.

might work(at least on Preview Release), but not permitted. Windows App Cert Kit says:

   Error: This application failed the supported API check.
   API CreateFileA in kernel32.dll is not supported for this application type. 
   API CreateFileW in kernel32.dll is not supported for this application type. 
   API DeleteFileA in kernel32.dll is not supported for this application type. 
    :
    :


回答2:

Try to open the database in the local folder. Here's a wrapper that works: http://sqlwinrt.codeplex.com/



回答3:

There was just released a new WinRT SQLite3 variant, that is compatible with the Windows Store guidelines. See https://github.com/doo/SQLite3-WinRT



回答4:

try this one (for UTF-8 database filenames only):

int ret = Sqlite3.sqlite3_open_v2("qq.db", out db, 1, "");

See sqlite.org for more details.



回答5:

I have not tried sqllite in WinRT, but I think it should work. The most likely candidate is that you do not have the correct permissions to the file you are trying to open. Check to make sure you can open the file with a simple API like fopen(). If that fails, then it is a permissions issue. You need to have your database in a directory that your app has natural rights to. It can't be in the user's documents folder for instance.