0

I can't seem to get off dead center with SQLite.Net-PCL; I have a Xamarin forms solution with a PCL proj, Droid proj, iOS proj, and Windows Universal project. I have installed the nuget packages for SQLite.Net-PCL and SQLite.Net.Core-PCL.

So, I go over to the github project to find some awesome examples to get started and none of them work; apparently you have to pass a platform into the database connection, but I get a reference error on them all (such as SQLitePlatformWin32).

Search the web for the references and... nothing. Search nuget for the platforms and... nothing.

What am I missing? (yes, I feel dumb)

An example they have is

 public class Database : SQLiteConnection
{
    public Database(string path) : base(new SQLitePlatformWin32(), path)
    {
        CreateTable<Stock>();
        CreateTable<Valuation>();
    }}

and I get a reference error that I can't resolve on the "new SQLitePlatformWin32" line.

1

1 Answer 1

1

For anyone else struggling, here is what you need to do.

  • Install the SQLite.Net-PCL, SQLite.Net.Core-PCL and SQLite.Net.Async-PCL nugets
  • Create an interface for ISQLite in your PCL:

    public interface ISQLite { SQLiteConnection GetConnection(); }

  • Call GetConnection via DependencyService

    PatientDatabase = DependencyService.Get<ISQLite>().GetConnection(); PatientDatabase.CreateTable<Patient>();

  • The above will create the connection based on your platform (i.e. android, ios). In each platform's project, you must have a GetConnection that is accessable via DependencyService such as this for Android

    [assembly: Xamarin.Forms.Dependency(typeof(SQLite_Android))] // ... public class SQLite_Android : ISQLite { public SQLite_Android() { } public SQLite.Net.SQLiteConnection GetConnection() { var sqliteFilename = "TodoSQLite.db3"; string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); // Documents folder var path = Path.Combine(documentsPath, sqliteFilename); // Create the connection var conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid(), path); // Return the database connection return conn; } }

My problem was I was trying to create the connection in my PCL and I couldn't find the platforms, what you need to do is create the connection in each individual project.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.