3

I am new to Xamarin and trying to create an app. When I try to create a SQLite database in Android below 7 then it's working perfectly. But when I am trying to create a database in 7 and 7.1 then I get this error. For this problem, see this link to the Xamarin discussion forum and I integrated the code:

 public SQLite.Net.SQLiteConnection GetConnection()
    {

        var sqliteFilename = "ADB.db3";
        string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var path = Path.Combine(documentsPath, sqliteFilename);

        if (!File.Exists(path))
        {
            using (var br = new BinaryReader(Android.App.Application.Context.Assets.Open("ATSDB.db3")))
            {
                using (var bw = new BinaryWriter(new FileStream(path, FileMode.Create)))
                {
                    byte[] buffer = new byte[2048];
                    int length = 0;
                    while ((length = br.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        bw.Write(buffer, 0, length);
                    }
                }
            }
        }



        // var plat = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
        var plat = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroidN();

        var conn = new SQLite.Net.SQLiteConnection(plat, path);
        // Return the database connection 
        return conn;

    }

**at this line I am getting Error ** var conn = new SQLite.Net.SQLiteConnection(plat, path); // Return the database connection

I am getting below error :

{System.DllNotFoundException: libsqlite3_xamarin      
    at (wrapper managed-to-native) SQLite.Net.Platform.XamarinAndroid.SQLiteApiAndroidNInternal:sqlite3_open_v2 (byte[],intptr&,int,intptr)

    at SQLite.Net.Platform.XamarinAndroid.SQLiteApiAndroidN.Open (System.Byte[] filename, SQLite.Net.Interop.IDbHandle& db, System.Int32 flags, System.IntPtr zvfs) [0x00000] in <360d65bfaa3e44c0b2b5840d827a452d>: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.IDictionary`2[TKey,TValue] tableMappings, System.Collections.Generic.IDictionary`2[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.IDictionary`2[TKey,TValue] tableMappings, System.Collections.Generic.IDictionary`2[TKey,TValue] 
    extraTypeMappings, SQLite.Net.IContractResolver resolver) [0x00000] 
    in <8f2bb39aeff94a30a8628064be9c7efe>:0 
    at ATSDriver.Droid.SqliteService.GetConnection () [0x000b5] 
    in E:\Projects\DriverApp\Source\ATSDriver\ATSDriver\ATSDriver.Android\SqliteService.cs:52 

at ATSDriver.DataAccess..ctor () [0x00009] in E:\Projects\DriverApp\Source\ATSDriver\ATSDriver\ATSDriver\DataAccess.cs:17 }

Hope some one can help me to resolve this.

2 Answers 2

12

In my case (targeting Android 9.0), I noticed that the Debug build worked, while the Release build did not. That's often a sign the linker is discarding too much. Adding Mono.Data.Sqlite to the 'Ignore assemblies' linker options did the trick for me:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

This is a bug with Sqlite.Net see a list of bugs about this here in particular this one. Android restricted access to files after Android 7 and this affected the Sqlite.Net package

In short the package Sqlite.Net is dead as the owner has not updated it to work with Android 7. Ref: https://github.com/oysteinkrog/SQLite.Net-PCL/issues/370

I suggest you switch to Sqlite-Net which supports Android 7+

2 Comments

thanks for your comments, is any example link for this for how can i use this because i have completed all the part of this app and in last i am getting such this very painful error
@A.Goutam Theres's no guide to doing this. its just a case of remove the old Sqlite.Net nuget package and install Sqlite-Net nuget package instead. Then build and fix any errors. Most of the errors will simply be changing a namespace. It's a pain but all users of Sqlite.Net have been there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.