Reputation: 125
I'm building a new project using Xamarin.Forms and I wanted to use SQLite to store the user data. I'm using .Net standard project. I installed the sqlite-net-pcl NuGet package by Frank A. Krueger in all 4 projects (shared code, droid, ios and uwp). I created the IFileHelper interface and the class implementing that interface in all projects, just as the documentation tells you to do. When I first launch the app I want to insert some default values in the database, but I just get an exception. I've been debugging and when the database is created on the App.xaml.cs doc it just returns null, so I don't get any database.
I've checked all code over and over and everything is fine, so I'm just wondering is sqlite-net-pcl compatible with .NET standard projects? Is there any solution for this prob?
This is the code on App.xaml.cs
static AppDatabase database;
public App ()
{
InitializeComponent();
MainPage = new MainPage();
}
public static AppDatabase Database
{
get
{
if (database == null)
{
database = new AppDatabase(DependencyService.Get<IFileHelper>().GetLocalFilePath("AppDatabase.db3"));
System.Threading.Thread.Sleep(500);
}
return database;
}
}
I when I return the database, it just returns null, so when I create the table on the AppDatabase class, I get the exception System.AggregateException: One or more errors occurred.
readonly SQLiteAsyncConnection Database;
public AppDatabase(string dbPath)
{
Database = new SQLiteAsyncConnection(dbPath);
Database.CreateTableAsync<Stats>().Wait();
}
I understand the exception, since the database is null I can't aggregate any table or anything at all since the database doesn't exist, but why it doesn't create the database?
This is the interface for the shared code
namespace FitApp
{
public interface IFileHelper
{
string GetLocalFilePath(string fileName);
}
}
And the class implementing the interface on the droid project:
[assembly: Dependency(typeof(FitApp.Droid.FileHelper))]
namespace FitApp.Droid
{
public class FileHelper : IFileHelper
{
public string GetLocalFilePath(string fileName)
{
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
return System.IO.Path.Combine(path, fileName);
}
}
}
Thanks
Upvotes: 2
Views: 2327
Reputation: 595
I found this page to really help in getting started with SQLite with Xamarin. Xamarin - Working with Local Databases in Xamarin.Forms Using SQLite
Note this page is not using the async
database.
My implementation which works. Initialisation;
public SQLiteAsyncConnection AsyncDb { get; private set; }
public App ()
{
InitializeComponent();
AsyncDb = DependencyService.Get<IDatabaseConnection>().AsyncDbConnection();
}
Interface;
public interface IDatabaseConnection
{
SQLite.SQLiteAsyncConnection AsyncDbConnection();
}
Droid Implementation;
public class DatabaseConnection_Droid : IDatabaseConnection
{
public SQLiteAsyncConnection AsyncDbConnection()
{
var dbName = "DiceyData.db3";
var path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), dbName);
return new SQLiteAsyncConnection(path);
}
}
Then you can operate on the appwide property, eg;
await ((App)Application.Current).AsyncDb.CreateTableAysnc<YourTypeHere>();
Upvotes: 1