Home > Blockchain >  Crashing/error on opening SQLite database in UWP/C#
Crashing/error on opening SQLite database in UWP/C#

Time:02-18

I'm trying to add code to an old UWP app to access an SQLite database. It works fine when debugging in VisualStudio but when I package it for sideloading it breaks.

I've shrunk it down and tried to simplify the code to see if there's any issues. Below is a small bit of code I'd put in to test out what is going on. The code breaks on opening the database. Again, this works fine when debugging in Visual Studio and breaks when sideloaded.

string entry = "";
string dbpath = @"S:\TestDB.db");
using (SqliteConnection db = new SqliteConnection($"Filename={dbpath}"))
{
    db.Open();
    string textCommand = "SELECT Source from Materials WHERE Material = @keyValue";
    SqliteCommand selectCommand = new SqliteCommand(textCommand, db);
    selectCommand.Parameters.AddWithValue("@keyValue", "PMMA");
    SqliteDataReader query = selectCommand.ExecuteReader();
    while (query.Read()) { entry = query.GetString(0); }
    db.Close();
}
return entry;

Does anyone have anything I can try out?

CodePudding user response:

Crashing/error on opening SQLite database in UWP/C#,'Unable to open database file' Is there a way I can get more info out of it.

I'm afraid you can't access db file where in the S disk with path directly. In UWP it only has permission access local folder with path. So we suggest you place your db file into LocalFolder like this document.

string dbpath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "sqliteSample.db");
    using (SqliteConnection db =
      new SqliteConnection($"Filename={dbpath}"))

CodePudding user response:

I found someone else who had a similar problem while searching for the answer to my question. I messaged them and they told me to put:

<rescap:Capability Name="developmentModeNetwork" /> 

in my manifest.

Seemed to do the trick.

  • Related