天天看點

SQLite出現attempt to write a readonly database解決

拿C#寫UWP通路資料庫的時候出現了問題,

attempt to write a readonly database

,一般是由于通路權限的的限制,最簡單的辦法就是把要用的資料庫複制到

LocalFolder

裡面

這屑問題折騰了我一下午(

//複制檔案,執行一次就夠了,用完即删
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("your-file-path"));
await file.CopyAsync(localFolder, "any name", NameCollisionOption.FailIfExists);
           
//用這個方式打開資料庫
string dbpath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "abc.db");
SQLiteConnection connection = new SQLiteConnection($"Data Source={dbpath}")