SqliteConnection doesnt completely open database in Xamarin

Greetings:

I am new to Xamarin and so far it is great. However, I am now in my fifth day of banging my head against the wall trying to get a simple Sqlite database to open. I am testing this on an Android phone, Samsung Galaxy S5. I am using the NuGet sqlite-net-pcl 1.5.231.

In PluralSight and in many other examples I have seen on the Web this should work; and I haven't yet found anyone else experiencing this issue.

If anyone has any idea at all, I am ready to try it. My head really hurts!

THE SYMPTOM
When I issue the SQLiteConnection connection = new SQLiteConnection(path); the file gets opened (else I get a not found condition) but SQLite acts like the datebase is empty. It cant find the tables.

Of course Sqlite sensing that this is an empty database handles it as a new empty database and thus returns the error that 'No such table exists: SITE'.

If I run the same code in a Dot.Net Console app pointing to the PC copy of that same database it works fine.
If I inspect the database on the phone using 'SQLite Manager for Android' from the Play store, it looks fine.

SOME EVIDENCE
I noticed that when inspecting the 'connection' variable right after its creation, there are two properties that are not initialized on the Xamarin example that are populated in the Dot.Net console app.

1) the BusyTimeout property is uninitialized on the Xamarin example while the Console App has a value.
2) the Tracer property is uninitialized on the Xamarin example while the Console App has a value.

These two differences are meaningless to the app; but perhaps some evidence that the connection is not fully completed .

{
public class Site
{
public string Name { get; set; }
public string Address { get; set; }
}

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SitesPage : ContentPage

{ 
    SitesViewModel viewModel;
    public SitesPage ()
    {
        InitializeComponent ();
        string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "KeyMaster.db");
        SQLiteConnection connection = new SQLiteConnection(path);
        List<Site> x = connection.Query<Site>("SELECT * FROM SITE");

        BindingContext = x;
    }

Answers

  • kentuckerkentucker USMember ✭✭✭✭✭

    How did you get the database on the phone to use?

  • RobbMRobbM Member ✭✭
    edited April 21

    Well, my newness to both Xamarin and SqLite was my undoing. My code was not finding the KeyMaster.db file and thus generated a new, empty one. Hence the symptom. I was hoping to be able to open a db that lived outside of my application; but that proved to be too much to tackle right now. So the following path gets my database opened:

    var dbName = "KeyMaster.db3";
    var x = System.IO.Path.Combine((string)Android.OS.Environment.ExternalStorageDirectory, "Android/data/com.osd.KeyMaster/files");

    I am moving forward; but now experiencing other semi-showstoppers such as the simple ability for a Modal to return a value to a caller; or at least a clean, simple callback event in the parent. Something that I would think would be fundamental. It's sad, because Xamarin is a great idea. It would be nice if it was given the support it deserves.

Sign In or Register to comment.