gpt4 book ai didi

c# - SQLite 中的 MissingMethodException

转载 作者:行者123 更新时间:2023-12-02 01:50:38 26 4
gpt4 key购买 nike

我试图让 SQLite 在本地存储一些信息,现在我遇到了一个恼人的问题:每当我调用 sqlite 函数 CreateTable 时,它​​都会返回一个异常。

这是一个异常(exception):

Unhandled Exception:
System.MissingMethodException: Method not found: string SQLitePCL.raw.sqlite3_column_name(SQLitePCL.sqlite3_stmt,int).

这是我用来使其跨平台的界面:

public interface IDataBase
{
SQLiteConnection GetConnection();
}

Android 实现:

[assembly: Dependency(typeof(DataBase))]
namespace MyNameSpace.Droid
{
public class DataBase : IDataBase
{
public DataBase() { }
public SQLiteConnection GetConnection()
{
var fileName = "DataBase.db3";
string docsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string path = Path.Combine(docsPath, fileName);
var conn = new SQLiteConnection(path);
return conn;
}
}
}

还有 iO 版:

[assembly: Dependency(typeof(DataBase))]
namespace MyNameSpace.iOS
{
public class DataBase : IDataBase
{
public DataBase() { }
public SQLiteConnection GetConnection()
{
var fileName = "DataBase.db3";
var docPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var libraryPath = Path.Combine(docPath, "..", "Library");
var path = Path.Combine(libraryPath, fileName);
var conn = new SQLiteConnection(path);
return conn;
}
}
}

然后我有我的表类型:

namespace MyNameSpace.DataBase
{
public class MacroCategoria
{
[PrimaryKey]
public int ID { get; set; }
public string descrizione { get; set; }

public MacroCategoria() { }

}
}

获取第一个元素的数据库 Controller 和 I 方法:

namespace MyNameSpace.DataBase
{
public class DBController
{
static object locker = new object();
SQLiteConnection database;

public DBController()
{
database = DependencyService.Get<IDataBase>().GetConnection();
if (database != null)
{
database.CreateTable<MacroCategoria>();
database.Insert(new MacroCategoria() { ID = 0, descrizione = "Description" });
}
}

public MacroCategoria getFirstMacro()
{
lock (locker)
{
if (database.Table<MacroCategoria>().Count() == 0)
return null;
else
return database.Table<MacroCategoria>().First();
}

}

}
}

在App类中调用:

public partial class App : Application
{
static DBController dbController;
public App()
{
InitializeComponent();
DependencyService.Register<MockDataStore>();
MainPage = new MainPage();
}

[...Other Default Methods...]

public static DBController DBController
{
get
{
if(dbController == null)
{
dbController = new DBController();
}
return dbController;
}
}
}

最后,当在我的主页中时,我有一个应该获取第一个元素的函数:

private void CartIcon_Clicked(object sender, EventArgs e)
{
try
{
App.DBController.getFirstMacro();

} catch(Exception ex) { throw; }
}

最佳答案

看起来我只需要添加 SQLitePCL.Raw 包。

关于c# - SQLite 中的 MissingMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57785849/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com