Linux/Mono(C#) - System.Data.SQLite- Sqlite.Interop.dll not found 问题

.Net工程在Ubuntu 14.04中使用mono运行,发现数据库相关报错:

ManagerDB.cs exc=System.DllNotFoundException: SQLite.Interop.dll
  at (wrapper managed-to-native) System.Data.SQLite.UnsafeNativeMethods.sqlite3_config_none(System.Data.SQLite.SQLiteConfigOpsEnum)
  at System.Data.SQLite.SQLite3.StaticIsInitialized () [0x0001d] in <10695578209d406fa38cf712bf41105d>:0 
  at System.Data.SQLite.SQLiteLog.Initialize (System.String className) [0x00026] in <10695578209d406fa38cf712bf41105d>:0 
  at System.Data.SQLite.SQLiteConnection..ctor (System.String connectionString, System.Boolean parseViaFramework) [0x00046] in <10695578209d406fa38cf712bf41105d>:0 
  at System.Data.SQLite.SQLiteConnection..ctor (System.String connectionString) 

检查发现bin包下有两个文件:

  • System.Data.SQLite.dll
  • SQLite.Interop.dll

查找相关资料后,发现还是在指定linux环境下编译相关的库文件比较靠谱,这样在Windows和Linux平台都能共同使用,最多可以再额外写个配置,用于不同平台、结构下的指定启动,但为了快速解决问题,暂时直接在目标linux平台编译:

下载源码

System.Data.SQLite: Downloads Page 官方地址下载对应的源代码(相关内容可能发生变化,以网站实际内容为准):


List of Release Packages
Source Code
 	sqlite-netFx-source-1.0.112.0.zip
(6.63 MiB)		This ZIP archive contains all current source code for System.Data.SQLite 1.0.112.0 (3.30.1) combined into a single archive file.
(sha1: 8ea203781925472c0dbb1c8b9e8b8f7a3575f7bc)
 	sqlite-netFx-full-source-1.0.112.0.zip
(13.06 MiB)		This ZIP archive contains all current source code for System.Data.SQLite 1.0.112.0 (3.30.1) and the extra files needed to run the unit test suite, combined into a single archive file.
(sha1: 7ea090b8a283090372d4dc282ff109c7bebb40a4)

编译sqlite-netfx-full-source

参考指令如下:

sudo apt-get update
sudo apt-get install build-essential
cd <source root>/Setup
chmod +x compile-interop-assembly-release.sh
./compile-interop-assembly-release.sh

编译完成后,在<source root>/bin/2013/Release/bin目录可以看到两个文件:

  • libSQLite.Interop.so
  • SQLite.Interop.dll

默认貌似都有可执行权限,可以将libSQLite.Interop.so的可执行权限取消:

chmod -x libSQLite.Interop.so

使用libSQLite.Interop.so

将编译生成的libSQLite.Interop.so拷贝至指定运行目录,执行成功,数据库正常加载。

参考资料

原英文内容ShortCut

Using System.Data.SQLite under Linux and Mono

If you want to use the lean, mean and fast SQLite database in your .NET programs under Linux and Mono, using Mono.Data.SQLite might seem like the obvious choice, but you can actually do better. With a little effort, you can use System.Data.SQLite under Linux+Mono (and probably macOS too). There a couple of key benefits to be gained from this:

  • If you build SQLite.Interop on the Mono machine, you get functionality not present in Mono.Data.SQLite, like SQLite virtual table support (see this answer from Joe Mistachkin).
  • The System.Data.SQLite NuGet package is actively maintained, whereas Mono.Data.SQLite is rarely updated. As of October 2016, the former is about a month old, but the latter is older than 1.5 years. This means that bug fixes (e.g. DateTime conversion throwing exceptions when columns have time zone info) remain unreleased for Mono.Data.SQLite.
  • In Mono.Data.SQLite, classes don’t have the same capitalization as System.Data.SQLite, so you can’t use the same source code as you do in .NET and just leave both the Mono.Data.SQLite and the System.Data.SQLite DLLs in your program directory. You’d have to resort either to #defines and conditional compilation or to using Mono.Data.SQLite under both .NET and Mono.

Building System.Data.SQLite Interop under Linux

There’s no System.Data.SQLite package for Linux, so you’ll have to build it yourself on your target Linux machine. You can build using this procedure, which is tested in Raspbian Jessie on a Raspberry Pi 3 and Ubuntu 16.04.1 on a PC:

  • Download System.Data.SQLite full source code from this download page. There’s a ton of files there, and the one you should look for is named something like sqlite-netFx-full-source-<version no>.zip.
  • Unzip it and transfer it to a directory on your Linux machine. In the rest of this description, I’ll call this directory “<source root>”.
  • Issue these commands in a Linux terminal:
sudo apt-get update
sudo apt-get install build-essential
cd <source root>/Setup
chmod +x compile-interop-assembly-release.sh
./compile-interop-assembly-release.sh
  • Now, you will have a freshly built library file called libSQLite.Interop.so in the <source root>/bin/2013/Release/bin directory. This file might have execution permission which isn’t relevant for a library, so remove it by
    chmod -x <source root>/bin/2013/Release/bin/libSQLite.Interop.so
  • Copy libSQLite.Interop.so the directory where your Mono/.NET application’s binaries reside (not the x64 or x86 subdirectories containing SQLite.Interop.dll), and you’re set to go.

And that, ladies and gentlemen, is how we do that!

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江湖大老弟

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值