0

ファイル保存ピッカーを使用して作成した後、sqlite データベースに接続できないのはなぜですか?

次のコードは、ファイル保存ピッカーを使用して sqlite データベースを作成するために機能します。最初にアプリフォルダーにデータベースを作成してから、ピッカーファイルにコピーする必要がありました。

        var savePicker = new FileSavePicker
{
    SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
    CommitButtonText = "Create new project"
};
        savePicker.FileTypeChoices.Add("Data files", new List<string> { ".db" });
        savePicker.DefaultFileExtension = ".db";

        pickedFile = await savePicker.PickSaveFileAsync();

        if (pickedFile != null)
        {
            Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(pickedFile);

            var dbLocalPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "temp.db");

            var db = new SQLiteAsyncConnection(dbLocalPath);
            await db.CreateTableAsync<myTable>();

            await db.ExecuteAsync("INSERT INTO myTable (Record) VALUES ('1')");

            Debug.WriteLine(dbLocalPath);

            var db2 = new SQLiteAsyncConnection(dbLocalPath);
            var allRecords = await db2.QueryAsync<qvdb>("SELECT * FROM myTable");
            var countRecords = allRecords.Any() ? allRecords.Count : 0;
            Debug.WriteLine("There are " + countRecords);  // this works

            var localFile = await ApplicationData.Current.LocalFolder.GetFileAsync("temp.db");
            if (localFile != null) await localFile.CopyAndReplaceAsync(pickedFile);  // this works can open and see table in SQLite db browser

            Debug.WriteLine(pickedFile.Path);  // path is correct

            var db3 = new SQLiteAsyncConnection(pickedFile.Path);

            // ERROR //

            //   A first chance exception of type 'myApp.SQLiteException' occurred in myApp.exe
            //   A first chance exception of type 'myApp.SQLiteException' occurred in mscorlib.dll
            //   myApp.SQLiteException: Could not open database file: C:\Users\Gary\Documents\sql1.db (14)
            //   at myApp.SQLiteConnection..ctor(String databasePath) in c:\Users\Gary\Desktop\Current\myApp\SQLite.cs:line 108
            //   at myApp.SQLiteConnectionWithLock..ctor(SQLiteConnectionString connectionString) in c:\Users\Gary\Desktop\Current\myApp\SQLiteAsync.cs:line 441
            //   at myApp.SQLiteConnectionPool.Entry..ctor(SQLiteConnectionString connectionString) in c:\Users\Gary\Desktop\Current\myApp\SQLiteAsync.cs:line 373
            //   at myApp.SQLiteConnectionPool.GetConnection(SQLiteConnectionString connectionString) in c:\Users\Gary\Desktop\Current\myApp\SQLiteAsync.cs:line 406
            //   at myApp.SQLiteAsyncConnection.GetConnection() in c:\Users\Gary\Desktop\Current\myApp\SQLiteAsync.cs:line 44
            //   at myApp.SQLiteAsyncConnection.<>c__DisplayClass26`1.<QueryAsync>b__25() in c:\Users\Gary\Desktop\Current\myApp\SQLiteAsync.cs:line 255
            //   at System.Threading.Tasks.Task`1.InnerInvoke()
            //   at System.Threading.Tasks.Task.Execute()
            //--- End of stack trace from previous location where exception was thrown ---
            //   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            //   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            //   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
            //   at myApp.Pages.MainPage.<btnStartNew_Click>d__c3.MoveNext() in c:\Users\Gary\Desktop\Current\myApp\Pages\MainPage.xaml.cs:line 1587
        }
4

0 に答える 0