Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
854 views
in Technique[技术] by (71.8m points)

windows runtime - Exception when using SQLite in WinRT app

I'm building a Windows 8 app, and now I want to use SQLite for this app. I installed SQLite for Windows Runtime through the Visual Studio 2013 extension manager, and I added sqlite-net to my project through NuGet.

I'm trying to create a database in my app.xaml.cs OnLaunched, but I get this exception when I run the project:

Unable to load DLL 'sqlite3': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

This is very strange because there is no error during compiling. Anyway, I think it tries to tell me that I need to reference an additional DLL: sqlite3.dll, but that doesn't work. I have 6 different DLLs on my file system: both debug and release versions of ARM, x64 and x86. I tried adding the x86 release version to my project but that results in this exception:

A reference to 'C:UsersLeonDocumentsVisual Studio 2013ProjectsGoogalyticspackagesSQLitex86sqlite3.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.

It's very sad that the documentation for sqlite-net sucks, it's very outdated (examples don't even work anymore), it's very incomplete and there is no mention of manually adding a DLL either. So I have 2 questions:

  1. How do I fix this particular issue?
  2. Where do I find up to date documentation for sqlite-net?

Edit: the code I use to create the DB is:

private void InitializeDatabase()
{
    var db = new SQLiteConnection("Googalytics");

    db.CreateTable<Account>();
    db.CreateTable<WebProperty>();
    db.CreateTable<Profile>();
}

I call that method from here:

InitializeDatabase();

if (rootFrame.Content == null)
{
    // When the navigation stack isn't restored navigate to the first page,
    // configuring the new page by passing required information as a navigation
    // parameter
    if (!rootFrame.Navigate(typeof(MainPage), args.Arguments))
    {
        throw new Exception("Failed to create initial page");
    }
}
// Ensure the current window is active
Window.Current.Activate();

edit2: some more info about my setup:

  • Visual Studio 2013 RC
  • Windows 8.1 RTM
  • SQLite for Windows Runtime 3.8.0.2
  • sqlite-net 1.0.7
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your project has its build mode currently set to Any CPU, what is the default. Because the SqLite assembly is not build as AnyCPU you need to set your build mode to X86 and add the X86 SqLite reference.

When deploying your app you also need to create 3 packages instead of 1 AnyCPU package.

Because your project is AnyCPU you get the error message when trying to add the x86, x86 is not valid for AnyCPU.


UPDATE

I tried to replicate your problem. I installed the SQLite for Windows Runtime for Visual Studio Ultimate 2012, after that I created a Windows Store Project, then added the SqLite reference after that I added sqlite-net and last I added your code for DB creation.

I modified the code a little bit (path & tables). But my code gives no error at all.

I did not need to reference the SqLite assemblies myself. Because by installing the extension into Visual Studio you get the reference in your extension list (still need to select it, just not add the dlls):

add reference

But still like I said in my first answer, you need to set your build mode to something else than 'Any CPU'

My example is on my skydrive (when testing set configuration to x86).


UPDATE 2

Db path:

var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");
var db = new SQLite.SQLiteConnection(dbPath);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...