在Android 7 +中获取数据库连接时出错

时间:2017-10-09 13:36:21

标签: sqlite xamarin.forms

我是Xamarin的新手,并试图创建一个APP。当我尝试在7以下的Android中创建一个SQLite数据库时,它运行得很好。但是,当我尝试在7和7.1中创建数据库时,然后得到错误。对于这个问题, link in Xamarin discussion forum和我整合了代码

 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;

    }

**在此行我收到错误** var conn = new SQLite.Net.SQLiteConnection(plat, path); // Return the database connection

我收到以下错误:

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

希望有人可以帮助我解决这个问题。提前谢谢。

2 个答案:

答案 0 :(得分:1)

在我的情况下(针对Android 9.0),我注意到Debug版本有效,而Release版本无效。这通常是linker丢弃过多信号的标志。将Mono.Data.Sqlite添加到“忽略程序集”链接器选项对我有用:

enter image description here

答案 1 :(得分:0)

这是Sqlite.Net的错误,请查看有关此here的错误列表,特别是this one。在Android 7之后Android限制了对文件的访问,这影响了Sqlite.Net

简而言之,程序包Sqlite.Net已失效,因为所有者尚未对其进行更新以使用Android 7.参考:https://github.com/oysteinkrog/SQLite.Net-PCL/issues/370

我建议您切换到支持Android 7 +的Sqlite-Net