0

I'm getting the following err msg:

enter image description here

...when I try to write to my SQLite database like so:

private async Task InsertMapRecord(string mapName, string mapNotes)
{
    path = folder.Path;
    connStr = string.Format(connStrBase, path);
    try
    {
        SqliteConnection conn = new SqliteConnection(connStr);
        String query = "INSERT INTO CartographerMain " +
            "(MapName, MapNotes) " +
            "VALUES (@MapName, @MapNotes)";

        using (SqliteCommand cmd = new SqliteCommand(query, conn))
        {
            cmd.Parameters.AddWithValue("@MapName", mapName);
            cmd.Parameters.AddWithValue("@MapNotes", mapNotes);
            await conn.OpenAsync();
            int result = await cmd.ExecuteNonQueryAsync();

            if (result < 0)
            {
                MessageDialog dialog = new MessageDialog("Error inserting data into CartographerMain");
                await dialog.ShowAsync();
            }
        }
    }
    catch (SqliteException sqlex)
    {
        string sqlExcMsg = string.Format("{0} Inner Exception: {1} Stack Trace: {2}", sqlex.Message,    sqlex.InnerException, sqlex.StackTrace);
        MessageDialog dialog = new MessageDialog(sqlExcMsg, "InsertMapRecord");
        await dialog.ShowAsync();
    }
}

So, based on some info here, I checked the folder where the app is running. It was marked as readonly, as shown here:

enter image description here

...so I changed it to NOT readonly, applied the change, and saved it thus:

enter image description here

Yet, I still get that same err msg with the above code. What else am I missing?

UPDATE

Answering Selvin's question in the comment below, I am storing the db file like so:

StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
string connStrBase = @"Data source={0}\Cartographer.db";
string connStr = string.Empty;
string path = string.Empty;

. . .

private async Task InsertMapRecord(string mapName, string mapNotes)
{
    path = folder.Path;
    connStr = string.Format(connStrBase, path);
    try
    {
        SqliteConnection conn = new SqliteConnection(connStr);
    . . .

I added the db to the project, set its Copy to Output Directory property to "Copy if newer"

The db file's path is C:\Users\bclay\source\repos\CartographerYou\CartographerYou\Cartographer.db. I manually copied it to there.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    Check permissions on database file and folder it is in. Also, check if any other process using your database (be it your app instance, antivirus, windows archivation software etc). – eocron Dec 20 '20 at 14:24
  • Folder including all subfolders and files, has been made readable, as shown above. – B. Clay Shannon-B. Crow Raven Dec 20 '20 at 14:26
  • 1
    UWP apps cannot write wherever they want, please show us how you are building path to database – Selvin Dec 20 '20 at 14:26
  • @Selvin: Yes, I have been told the db needs to be in LocalFolder, but how can I programmatically place the db there is the question now. Please see https://stackoverflow.com/questions/65361052/why-is-my-sqlite-database-table-not-being-found-when-it-is-is-in-the-expected-l – B. Clay Shannon-B. Crow Raven Dec 20 '20 at 14:28
  • 3
    `var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///[location_to_db_from_appx_folder]"));` `await file.CopyAsync(ApplicationData.Current.LocalFolder);` This is to copy your existing database file to your `LocalFolder` – Leander Dec 20 '20 at 14:34
  • 1
    installation folder is readonly.. copy it as Leander wrote and use new path – Selvin Dec 20 '20 at 14:41
  • @Selvin: I get err msgs when I add that code. Will Update again with them. – B. Clay Shannon-B. Crow Raven Dec 20 '20 at 14:45
  • 1
    I might have made some typos, but this should work in general. If you then change your `StorageFolder folder = ...Current.InstalledLocation` to `StorageFolder folder = Windows.ApplicationData.Current.LocalFolder` you should be able to modify your database through Sqlite, without changing any other code. – Leander Dec 20 '20 at 15:18

0 Answers0