B.Pumpkin B.Pumpkin - 5 months ago 48
C# Question

SQLite in Xamarin throws exception in connection

I have a problem. I am using Xamarin for iOS and Android applications. But it always throws an exception when I try to build a connection to the folder path. This is my code:
public class Queries
{
private string folder = DependencyService.Get().GetAppDataFolder();

public bool createDataBase()
{
try
{
using (var connection = new SQLiteConnection(Path.Combine(folder, "Test.db")))
{
connection.CreateTable<Posten>();
return true;
}
}
catch (SQLiteException ex)
{
System.Diagnostics.Debug.WriteLine("SQLiteEx", ex.Message);
return false;
}
}
}


It throws the exception in the using. I use the DependencyService to get the path I want to put my database in.

private string folder = DependencyService.Get<IFileSystemService>().GetAppDataFolder();


on the android side (i'm currently just testing android) i have implemented it like this:

public class FileSystemServiceAndroid : IFileSystemService
{
public string GetAppDataFolder()
{
return System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
}
}


this is what the exception says:


Unhandled Exception:

System.TypeInitializationException: The type initializer for >'SQLite.SQLiteConnection' threw an exception. occurred


I just moved the code where I try to access the createDataBase method to where my toolbar is initialized and I got a message box in the application:
The Error

Answer Source

Well... I went on vacation. When I came back my coworker told me that he reinstalled the sqlite packages like 10 times and then it worked again... So I guess that's the answer