在 WinRT 应用程序中使用 SQLite 时出现异常 [英] Exception when using SQLite in WinRT app

查看:31
本文介绍了在 WinRT 应用程序中使用 SQLite 时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 Windows 8 应用程序,现在我想为这个应用程序使用 SQLite.我通过 Visual Studio 2013 扩展管理器安装了 SQLite for Windows Runtime,并通过 NuGet 将 sqlite-net 添加到我的项目中.

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.

我正在尝试在我的 app.xaml.cs OnLaunched 中创建一个数据库,但是当我运行该项目时出现此异常:

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

无法加载 DLL 'sqlite3':找不到指定的模块.(来自 HRESULT 的异常:0x8007007E)

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

这很奇怪,因为编译时没有错误.无论如何,我认为它试图告诉我我需要引用一个额外的 DLL:sqlite3.dll,但这不起作用.我的文件系统上有 6 个不同的 DLL:ARM、x64 和 x86 的调试和发布版本.我尝试将 x86 发布版本添加到我的项目中,但导致此异常:

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:

对'C:\Users\Leon\Documents\Visual Studio 的引用2013\Projects\Googalytics\packages\SQLite\x86\sqlite3.dll' 无法添加.请确保该文件可访问,并且它是有效的程序集或 COM 组件.

A reference to 'C:\Users\Leon\Documents\Visual Studio 2013\Projects\Googalytics\packages\SQLite\x86\sqlite3.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.

很遗憾,sqlite-net 的文档很烂,非常过时(示例甚至不再起作用),非常不完整,也没有提到手动添加 DLL.所以我有两个问题:

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. 我该如何解决这个特定问题?
  2. 在哪里可以找到 sqlite-net 的最新文档?

我用来创建数据库的代码是:

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>();
}

我从这里调用该方法:

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:有关我的设置的更多信息:

edit2: some more info about my setup:

  • Visual Studio 2013 RC
  • Windows 8.1 RTM
  • 适用于 Windows 运行时 3.8.0.2 的 SQLite
  • sqlite-net 1.0.7

推荐答案

您的项目的构建模式当前设置为 Any CPU,默认设置是什么.由于 Sqlite 程序集不是作为 AnyCPU 构建的,因此您需要将构建模式设置为 X86 并添加 X86 Sqlite 引用.

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.

部署应用时,您还需要创建 3 个包,而不是 1 个 AnyCPU 包.

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

因为您的项目是 AnyCPU,所以您在尝试添加 x86 时收到错误消息,x86 对 AnyCPU 无效.

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

我试图重现您的问题.我为 Visual Studio Ultimate 2012 安装了 SQLite for Windows Runtime,之后我创建了一个 Windows Store 项目,然后添加了 Sqlite 引用,然后添加了 sqlite-net 和最后我添加了您的数据库创建代码.

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.

我不需要自己引用 Sqlite 程序集.因为通过将扩展安装到 Visual Studio 中,您会在扩展列表中获得引用(仍然需要选择它,只是不添加 dll):

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):

但仍然像我在第一个回答中所说的那样,您需要将构建模式设置为Any CPU"以外的其他内容

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

我的例子是在我的 skydrive(测试设置配置为 x86 时).

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

数据库路径:

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

这篇关于在 WinRT 应用程序中使用 SQLite 时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆