类型或名称空间名称“平台”在名称空间“ SQLite.Net”中不存在

时间:2020-03-12 18:05:23

标签: sqlite xamarin.forms xamarin.android sqlite-net

我一直在研究一个可以使用的数据库示例,终于找到了一个并且正在尝试重新构建数据库,以便可以在需要的范围内使用它。

这是我正在使用的链接https://www.c-sharpcorner.com/UploadFile/f8fa6c/use-sqlite-net-in-xamarin-forms/

错误所在的代码:

import { FETCH_BITCOIN } from "./constants";

export const fetchBitcoin = () => {
  return dispatch => {
    return fetch("https://api.coindesk.com/v1/bpi/currentprice.json")
      .then(response => response.json())
      .then(json => dispatch({ type: FETCH_BITCOIN, bitcoin: json }));
  };
};

我试图建立的连接:

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);
        }
    }

1 个答案:

答案 0 :(得分:1)

您使用的代码在最新版本的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;
    }
}

您可以查看官方文档以了解更多信息:databasesusing-sqlite-orm

相关问题