Thomas Athanasiou
Thomas Athanasiou

Reputation: 121

SQLite Xamarin.Forms PCL errors on Android

I am trying to build an sqlite database on xamarin(C#) in pcl project. I am following this tutorial. On the Android Implementation (Step5) i get these errors:

Error CS0104 'Environment' is an ambiguous reference between 'Android.OS.Environment' and 'System.Environment' AlarmSQLite.Android c:\users\thomas\source\repos\AlarmSQLite\AlarmSQLite\AlarmSQLite.Android\SQLite_Android.cs 30 Active

Error CS0234 The type or namespace name 'Net' does not exist in the namespace 'SQLite' (are you missing an assembly reference?) AlarmSQLite.Android c:\users\thomas\source\repos\AlarmSQLite\AlarmSQLite\AlarmSQLite.Android\SQLite_Android.cs 33 Active

I use VisualStudio2017. I tried to remove .Net and added System.Environment but i get more and new errors. My code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using SQLite;
using Xamarin.Forms;
using AlarmSQLite.Droid;
using System.IO;

[assembly: Dependency(typeof(SQLite_Android))]

namespace AlarmSQLite.Droid
{
public class SQLite_Android : ISQLite
{
    public SQLite_Android() { }



    public SQLite.SQLiteConnection GetConnection()
    {
        var dbName = "AlarmDB.db3";
        var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        var path = Path.Combine(documentsPath, dbName);

        var platform = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
        var connection = new SQLite.Net.SQLiteConnection(platform, path);

        return connection;
    }

}
}

Everything is the same with the tutorial. What am i doing wrong? Thank you!

New Errors:

Error CS0234 The type or namespace name 'Platform' does not exist in the namespace 'SQLite' (are you missing an assembly reference?) AlarmSQLite.Android C:\Users\Thomas\source\repos\AlarmSQLite\AlarmSQLite\AlarmSQLite.Android\SQLite_Android.cs 33 Active

Error CS0029 Cannot implicitly convert type 'SQLite.Net.SQLiteConnection' to 'SQLite.SQLiteConnection' AlarmSQLite.Android C:\Users\Thomas\source\repos\AlarmSQLite\AlarmSQLite\AlarmSQLite.Android\SQLite_Android.cs 36 Active

Upvotes: 0

Views: 925

Answers (1)

MilanG
MilanG

Reputation: 2604

Error CS0104: Environment comes from 'Android.OS.Environment' as well as 'System.Environment' so giving you and ambiguity issue. Just Prepend System to the Environment.

var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

Error CS0234: Seems like you haven't added SQLite-Async Nuget Package. You have to add this in your PCL as well as in Android project and build project again.

Error CS0234 & CS0029: Make sure you added following two nuget packages in android and pcl projects.

Sqlite.Net PCL
enter image description here

Then, instead of using Sqlite, try to use Sqlite.Net.

enter image description here

Your Final Code should Look :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
using AlarmSQLite.Droid;
using System.IO;
using SQLite.Net;
using SQLite.Net.Async;

[assembly: Dependency(typeof(SQLite_Android))]

namespace AlarmSQLite.Droid
{
public class SQLite_Android : ISQLite
{
    public SQLite_Android() { }



    public SQLiteConnection GetConnection()
    {
        var dbName = "AlarmDB.db3";
        var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var path = Path.Combine(documentsPath, dbName);

        var platform = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
        var connection = new SQLiteConnection(platform, path);

        return connection;
    }

}
}

Do not forget to adjust your interface for the same. It should be like:

SQLiteConnection GetConnection():

Note: You can omit Sqlite.Net.Async PCL reference if you don't need it.

Upvotes: 1

Related Questions