5
votes

I am facing SQLite issue in xamarin forms project. However SQLiteConnection I am trying to instantiating in Android platform there getting exception.

Libraries using in android & iOS project

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

menifest file target versions

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

AndroidSQLite.cs file

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

Getting exception at

 var conn= new SQLiteConnection(plat, dpPath);

I have installed above libraries on solution level but in shared project, there is no option available to install Nuget packages. Shared project screen shot

enter image description here

Following this article to learn SQLite with xamarin xamarin forms-mvvm-sqlite-sample.

Full exception below

{System.DllNotFoundException: /system/lib/libsqlite.so at (wrapper managed-to-native) SQLite.Net.Platform.XamarinAndroid.SQLiteApiAndroidInternal.sqlite3_open_v2(byte[],intptr&,int,intptr) at SQLite.Net.Platform.XamarinAndroid.SQLiteApiAndroid.Open (System.Byte[] filename, SQLite.Net.Interop.IDbHandle& db, System.Int32 flags, System.IntPtr zvfs) [0x00000] in <8dbf6ff85082469fb9d4dfaa9eae6b69>:0 at SQLite.Net.SQLiteConnection..ctor (SQLite.Net.Interop.ISQLitePlatform sqlitePlatform, System.String databasePath, SQLite.Net.Interop.SQLiteOpenFlags openFlags, System.Boolean storeDateTimeAsTicks, SQLite.Net.IBlobSerializer serializer, System.Collections.Generic.IDictionary2[TKey,TValue] tableMappings, System.Collections.Generic.IDictionary2[TKey,TValue] extraTypeMappings, SQLite.Net.IContractResolver resolver) [0x000a2] in <8f2bb39aeff94a30a8628064be9c7efe>:0 at SQLite.Net.SQLiteConnection..ctor (SQLite.Net.Interop.ISQLitePlatform sqlitePlatform, System.String databasePath, System.Boolean storeDateTimeAsTicks, SQLite.Net.IBlobSerializer serializer, System.Collections.Generic.IDictionary2[TKey,TValue] tableMappings, System.Collections.Generic.IDictionary2[TKey,TValue] extraTypeMappings, SQLite.Net.IContractResolver resolver) [0x00000] in <8f2bb39aeff94a30a8628064be9c7efe>:0

3
Did you install the NuGet on both your platform and shared code project?Gerald Versluis
@Gerald Versluis- Packages I have added across projects(I selected 3 projects including shared project while adding packages). But it doesn't seem there is an option to install packages in shared project. See my updated question where I added screenshot of shared project.R15
Have you tried to use sqlite-net-pcl package instead?Bruno Caceiro
@BrunoCaceiro I haven't tried that before. But now when I installed that, after changing few namespaces getting error at SQLite.Net.Platform Platform is not available.R15
Use sqlite-net-pcl package: github.com/praeclarum/sqlite-net Lib you are using wasn't updated for 2 years.Access Denied

3 Answers

11
votes

The SQLite.Net-PCL nuget you used is no longer being maintained by it's author. You need to switch to other similar package sqlite-net-pcl

First remove the existing SQLite.Net-PCL nuget from all projects and install the one I mentioned above. Remove all old SQLite related references from all files.

You need to make some changes in SQLite connection related dependency service classes. Then you may adjust some other code which relates to SQLite. Resolve the references by adding appropriate using statements for the new nuget package.

I got the same error and I solved it by switching to above nuget package.

NOTE : The new nuget package does not have InsertOrReplaceAll method yet. So If you have used this method from previous nuget, then for the new nuget, you need to create an InsertOrReplaceAll extension method.

EDIT: To get the SQLiteConnection:

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

There is no need to pass the platform in this new nuget. Pass false if you don't want to store DateTime as Ticks.

1
votes

2 choices

  1. Change targetSdkVersion < 24, since Android 7, app can not access system private libraries.
  2. Add libsqlite.so into your own lib folder.
1
votes

Little late but for someone facing the same issue, you just need to add a below line in your native code

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

If this not work then uninstall your existing sqlite-net-pcl and install sqlite-net-pcl this library and try again