c# - SQLite System.DllNotFoundException: e_sqlite3 使用 sqlite-net-pcl
问题描述
您好,我正在尝试在 Xamarin Forms 项目中使用 SQLite。但我很难让它工作。我将提供有关我的项目配置的信息:
- Xamarin 版本-> 4.6.0.726
- 代码共享策略 -> .NET 标准
- sqlite-net-pcl 1.6.292
问题:当我尝试获取连接时抛出以下异常-> {system.typeinitializationexception:“sqlite.sqliteconnection”的类型初始化程序引发了异常。---> system.dllnotfoundexception: e_sqlite3 at (wrapper managed-to-native) sqlitepcl.sqlite3provider_e_sqlite3+nativemethods.sqlite3_libversion_number() at sqlitepcl.sqlite3provider_e_sqlite3.sqlitepcl.isqlite3provider.sqlite3_libversion_number () [0x00000] in :0 at sqlitepcl.raw。 setprovider (sqlitepcl.isqlite3provider imp) [0x00008] in :0 at sqlitepcl.batteries_v2.init () [0x00005] in :0 at sqlite.sqliteconnection..cctor () [0x00016] in <84b9c9e630fa45bd8ac799333976ebbf>:0 --- end内部异常堆栈跟踪 --- 在 sqlite.sqliteconnectionwithlock..ctor (sqlite.sqliteconnectionstring connectionstring) [0x0000b] in <84b9c9e630fa45bd8ac799333976ebbf>:0 at sqlite.sqliteconnectionpool+entry.connect () [0x0001c] in <
我做了深入的研究,但没有发现任何对我有用的东西。我访问的最后一篇文章是:Android - 'SQLite.SQLiteConnection' 的类型初始化程序引发了异常。---> System.DllNotFoundException: e_sqlite3接受的答案只是说在 android 项目上安装 nugget 将解决问题(我已经做过的事情)
我将展示一些代码以及我的掘金的外观。机器人包
<package id="sqlite-net-pcl" version="1.6.292" targetFramework="monoandroid90" />
<package id="SQLitePCLRaw.bundle_green" version="2.0.1" targetFramework="monoandroid90" />
<package id="SQLitePCLRaw.core" version="2.0.1" targetFramework="monoandroid90" />
<package id="SQLitePCLRaw.lib.e_sqlite3" version="2.0.1" targetFramework="monoandroid90" />
<package id="SQLitePCLRaw.lib.e_sqlite3.osx" version="1.1.14" targetFramework="monoandroid90" />
<package id="SQLitePCLRaw.provider.e_sqlite3" version="2.0.1" targetFramework="monoandroid90" />
共享代码项目包
<package id="sqlite-net-pcl" version="1.6.292" targetFramework="portable45-net45+win8+wpa81" />
<package id="SQLitePCLRaw.bundle_green" version="1.1.13" targetFramework="portable45-net45+win8+wpa81" />
<package id="SQLitePCLRaw.core" version="1.1.13" targetFramework="portable45-net45+win8+wpa81" />
<package id="SQLitePCLRaw.lib.e_sqlite3.osx" version="1.1.14" targetFramework="portable45-net45+win8+wpa81" />
用于获取路径的接口
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UISampleApp.Interfaces
{
public interface ISQLitePlatform
{
SQLiteConnection GetConnection();
SQLiteAsyncConnection GetConnectionAsync();
String GetPath();
}
}
机器人实现
using System;
using System.IO;
using Android.OS;
using SQLite;
using SQLitePCL;
using UISampleApp.Interfaces;
using UISampleApp.Models;
[assembly: Xamarin.Forms.Dependency(typeof(UISampleApp.Droid.Implementations.SQLitePlatform))]
namespace UISampleApp.Droid.Implementations
{
public class SQLitePlatform : ISQLitePlatform
{
public string GetPath() {
var dbName =Constantes.DatabaseFileName;
var path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),dbName);
return path;
}
public SQLiteConnection GetConnection()
{
return new SQLiteConnection(GetPath());
}
public SQLiteAsyncConnection GetConnectionAsync()
{
return new SQLiteAsyncConnection(GetPath());
}
}
}
生成异常的代码
string path = DependencyService.Get<ISQLitePlatform>().GetPath();
SQLite.SQLiteAsyncConnection x = DependencyService.Get<ISQLitePlatform>().GetConnectionAsync();
try
{
await x.Table<TodoItem>().ToListAsync();
}
catch (Exception e)
{
throw e;
}
如果需要更多信息,我希望有人可以帮助我,请告诉我,我会提供。PS:在使用 PCL 的共享代码策略工作的项目上做同样的事情,但我需要在使用 .NET 标准作为共享代码策略的项目中工作。谢谢
GitHub:https ://github.com/jesse0099/Hackathon2019-Movil.git
解决方案
推荐阅读
- javascript - 下拉菜单选项标签内的css线性渐变
- javascript - 如何编码“设置图像大小=点击次数+100”?
- java - 网格中的机器人 - DS
- python-3.5 - 我想我在 discord.py 机器人的逻辑中错误地添加了图像文件夹的路径
- python - 在 Windows 10 上安装 Python 包时出错
- css - 在css中访问全局变量
- java - Firestore 服务器身份验证:重叠文档
- mercurial - Mercurial,简化多头怪物
- javascript - 为什么这个承诺没有返回给调用者?
- module - 在 Julia 中创建和使用自定义模块