Interfacing SQLite Both Android & iOS

 Install Package

SQLite.Net-PCL 3.1.1




In PCL
------------------------------
 public interface ISQLite
    {
        SQLiteConnection GetConnection();
    }

-------------------------------------
In Xamarin.Android
-------------------------------------
[assembly:Xamarin.Forms.Dependency(typeof(LocationPOC.Droid.Implementations.SqlLiteAndroid))]
namespace
{
    class SqlLiteAndroid : ISQLite
    {
        public SQLiteConnection GetConnection()
        {
            var filename = "Locations.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;
        }
    }
}

-------------------------------------
In Xamarin.iOS
-------------------------------------

[assembly: Xamarin.Forms.Dependency(typeof(SqliteIOS))]
namespace
{
    public class SqliteIOS : ISQLite
    {
        public SQLiteConnection GetConnection()
        {
            var fileName = "Locations.db3";
            var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            var libraryPath = Path.Combine(documentsPath, "..", "Library");
            var path = Path.Combine(libraryPath, fileName);

            var platform = new SQLite.Net.Platform.XamarinIOS.SQLitePlatformIOS();
            var connection = new SQLite.Net.SQLiteConnection(platform, path);

            return connection;
        }
    }
}

Comments

Popular posts from this blog

Listview SelectedItem in MVVM

Button click event with current object from listview having button

SideMenu React native