我一直在研究一个可以使用的数据库示例,最终找到了一个并且正在尝试重新构造它,以便可以在需要的范围内使用它。
这里是我正在使用的链接https://www.c-sharpcorner.com/UploadFile/f8fa6c/use-sqlite-net-in-xamarin-forms/
错误所在的代码:
namespace PAPvolley.Droid
{
public class SQLite_Android : ISQLite
{
public SQLite.Net.SQLiteConnection GetConnection()
{
var filename = "Team.db3";
var documentspath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(documentspath, filename);
var platform = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
var connection = new SQLite.Net.SQLiteConnection(platform, path);
return connection;
}
}
}
正在尝试建立的连接:
public interface ISQLite
{
SQLiteConnection GetConnection();
}
我的数据库:
public class TeamDB
{
private SQLiteConnection _sqlconnection;
public TeamDB()
{
//Getting conection and Creating table
_sqlconnection = DependencyService.Get<ISQLite>().GetConnection();
_sqlconnection.CreateTable<Team>();
}
//Get all students
public IEnumerable<Team> GetTeam()
{
return (from t in _sqlconnection.Table<Team>() select t).ToList();
}
//Get specific student
public Team GetTean(int id)
{
return _sqlconnection.Table<Team>().FirstOrDefault(t => t.Id == id);
}
//Delete specific student
public void DeleteTeam(int id)
{
_sqlconnection.Delete<Team>(id);
}
//Add new student to DB
public void AddTeam(Team team)
{
_sqlconnection.Insert(team);
}
}
public class Team
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string TeamName { get; set; }
public Team()
{
}
}
您使用的代码在sqlite-net-pcl的最新版本中已过时。
要在Android项目中创建SQLiteConnection,请安装最新的sqlite-net-pcl
并执行以下步骤:
public class SQLite_Android : ISQLite
{
public SQLiteConnection GetConnection()
{
var filename = "Team.db3";
var documentspath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(documentspath, filename);
var connection = new SQLiteConnection(path);
return connection;
}
}
您可以查看官方文档以获取更多信息:databases和using-sqlite-orm