System.DllNotFoundException:/system/lib/libsqlite.so- Xamarin形式

时间:2018-08-28 07:46:09

标签: c# sqlite xamarin xamarin.forms

我在xamarin表单项目中面临SQLite问题。但是SQLiteConnection我正在尝试在Android平台上实例化那里出现异常。

在Android和iOS项目中使用的库

  • SQLite.Net-PCL 3.1.1
  • SQLite.Net.Core-PCL 3.1.1

清单文件目标版本

<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="27" />

AndroidSQLite.cs文件

using SQLite;
using SQLite.Net;
using System.IO;
namespace XamarinTestList.Droid.Implementations
{
public class AndroidSQLite : ISQLite
{
    public SQLiteConnection GetConnection()
    {
        string documentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var path = Path.Combine(documentPath, DatabaseHelper.DbFileName); //DbFileName="Contacts.db"
        var plat = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
        string dpPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "user.db3");
        var conn= new SQLiteConnection(plat, dpPath);
        return conn;
    }
}
}

获取异常
 var conn= new SQLiteConnection(plat, dpPath);

我已经在解决方案级别上安装了上述库,但是在共享项目中,没有可用于安装Nuget软件包的选项。共享项目的屏幕截图

enter image description here

在本文之后学习xamarin的SQLite xamarin forms-mvvm-sqlite-sample

下面是完全例外

  

{System.DllNotFoundException:/system/lib/libsqlite.so at(包装   本地管理)   SQLite.Net.Platform.XamarinAndroid.SQLiteApiAndroidInternal.sqlite3_open_v2(byte [],intptr&,int,intptr)   在SQLite.Net.Platform.XamarinAndroid.SQLiteApiAndroid.Open   (System.Byte []文件名,SQLite.Net.Interop.IDbHandle&db,   System.Int32标志,System.IntPtr zvfs)[0x00000]在   <8dbf6ff85082469fb9d4dfaa9eae6b69>:0在   SQLite.Net.SQLiteConnection..ctor(SQLite.Net.Interop.ISQLitePlatform   sqlitePlatform,System.String databasePath,   SQLite.Net.Interop.SQLiteOpenFlags openFlags,System.Boolean   storeDateTimeAsTicks,SQLite.Net.IBlobSerializer序列化器,   System.Collections.Generic.IDictionary 2[TKey,TValue] tableMappings, System.Collections.Generic.IDictionary 2 [TKey,TValue]   extraTypeMappings,SQLite.Net.IContractResolver解析器)[0x000a2]在   <8f2bb39aeff94a30a8628064be9c7efe>:0在   SQLite.Net.SQLiteConnection..ctor(SQLite.Net.Interop.ISQLitePlatform   sqlitePlatform,System.String databasePath,System.Boolean   storeDateTimeAsTicks,SQLite.Net.IBlobSerializer序列化器,   System.Collections.Generic.IDictionary 2[TKey,TValue] tableMappings, System.Collections.Generic.IDictionary 2 [TKey,TValue]   extraTypeMappings,SQLite.Net.IContractResolver解析器)[0x00000]在   <8f2bb39aeff94a30a8628064be9c7efe>:0

3 个答案:

答案 0 :(得分:4)

您使用的SQLite.Net-PCL nuget不再由其作者维护。您需要切换到其他类似的软件包sqlite-net-pcl

首先从所有项目中删除现有的SQLite.Net-PCL nuget,然后安装我上面提到的那个。从所有文件中删除所有旧的SQLite相关参考。

您需要在与连接相关的SQLite相关的依赖项服务类中进行一些更改。然后,您可以调整与SQLite相关的其他代码。通过为新的nuget包添加适当的using语句来解析引用。

我遇到了同样的错误,我通过切换到上述nuget包解决了该问题。

注意:新的nuget程序包还没有InsertOrReplaceAll方法。因此,如果您从以前的nuget中使用了此方法,那么对于新的nuget,您需要创建一个InsertOrReplaceAll extension method

编辑:要获取SQLiteConnection,

public SQLiteConnection GetConnection()
{
    return new SQLiteConnection(dpPath, false);
}

无需在此新的nuget中传递平台。如果您不想将DateTime存储为Ticks,请传递false

答案 1 :(得分:1)

为时已晚,但对于面临相同问题的人,您只需在本机代码中添加以下一行

[assembly: Xamarin.Forms.Dependency(typeof(AndroidSQLite))]

如果这不起作用,请卸载现有的sqlite-net-pcl并安装sqlite-net-pcl此库,然后重试

答案 2 :(得分:1)

2个选择

  1. 更改targetSdkVersion <24,因为Android 7,应用无法访问系统私有库。
  2. 将libsqlite.so添加到您自己的lib文件夹中。