Search code examples
sqlitexamarin.formsxamarin.androidsqlite-net

The type or namespace name 'Platform' does not exist in the namespace 'SQLite.Net'


I have been researching for a example of a database what I could use and I finally found one and I'm trying to reformulate it, so that I can use it in the extent that I want.

Here is the link to which I am using https://www.c-sharpcorner.com/UploadFile/f8fa6c/use-sqlite-net-in-xamarin-forms/

The code where the error is:

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

The connection im trying to make:

public interface ISQLite
{
     SQLiteConnection GetConnection();    
}

My DB:

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

Solution

  • The codes you use is out of date in the latest version of sqlite-net-pcl.

    To create a SQLiteConnection in Android project, install the latest sqlite-net-pcl and do following steps:

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

    You can check the official document for more information: databases and using-sqlite-orm