2016-03-05 46 views
0

概述 - 我已經添加了一些代碼將現有的數據庫複製到設備的本地文件夾。到目前爲止第一個條件如果現有的分貝不存在工作正常。如何解決不正確的SQLite數據庫路徑?

問題 - 但是,當代碼行現有的數據庫從解決方案文件夾複製到設備文件夾執行,我得到一個SQLite錯誤。該錯誤告訴我,db文件無法打開。

在調試期間,我看到DBPath與我的解決方案中的文件位置相同。所以我不太確定這條道路會出現什麼問題。

(該數據庫連接的內容,並設置爲「一直拷貝」)

在包中的數據庫文件的完整路徑是:

C:\Users\Brian\Documents\Visual Studio 2013\Projects\Parking Tag Picker WRT\Parking Tag Picker WRT\Databases\ParkingZoneDatabase.db 

問題:我怎麼能解決數據庫路徑到SQLite類所需的正確路徑?

enter image description here

錯誤日誌 - 那得到的位置拋出是在SQLite的類遵循異常的具體轉儲:

SQLite.SQLiteException was unhandled by user code 
    HResult=-2146233088 
    Message=Could not open database file: C:\Data\Users\DefApps\APPDATA\Local\Packages\6d00c25c-39d2-443f-a29b-2c30c8ce7e99_gevy8cezwa384\LocalState\Databases\ParkingZoneDatabase.db (CannotOpen) 
    Source=Parking Tag Picker WRT 
    StackTrace: 
     at SQLite.SQLiteConnection..ctor(String databasePath, SQLiteOpenFlags openFlags, Boolean storeDateTimeAsTicks) 
     at SQLite.SQLiteConnection..ctor(String databasePath, Boolean storeDateTimeAsTicks) 
     at Parking_Tag_Picker_WRT.Helpers.DatabaseHelper.ReadZones(String tableName) 
     at Parking_Tag_Picker_WRT.ViewModel.TagRequestViewModel.InitZoneInfoAsync() 
     at Parking_Tag_Picker_WRT.TagRequestPage.OnNavigatedTo(NavigationEventArgs e) 
    InnerException: 

DBHelper代碼:(代碼複製現有db到設備上的本地文件夾)

public const string DBPath = @"Databases\ParkingZoneDatabase.db"; 

    /// <summary> 
    /// Load SQL_LiteTable from Solution 
    /// </summary> 
    /// <param name="DBPATH"></param> 
    /// <returns></returns> 
    public async Task<bool> Init() 
    { 


     bool isDatabaseExisting = false; 

     try 
     { 
      StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(DBPath); 
      isDatabaseExisting = true; 
     } 
     catch 
     { 
      isDatabaseExisting = false; 
     } 

     if (!isDatabaseExisting) 
     { 
      //Fails at this line when retrieving the existing db 
      StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync(DBPath); 
      await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder); 
     } 

     return true;   
    } 

我還檢查的權限對數據庫文件本身,這設置爲只讀 -

enter image description here

+0

您是否在同步條件下檢查是否發生相同的異常? –

+1

'公共異步任務初始化() { StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync(DBPath); 嘗試 等待databaseFile.CopyAsync(ApplicationData.Current。LocalFolder); } catch {} }' 您可以導航到您的數據庫文件夾,右鍵單擊您的數據庫並檢查其安全性 - 讀寫權限。如果仍然無法解決,您是否可以共享發生此錯誤的示例項目。 – Jerin

+0

「數據庫」文件夾是否存在?也許你需要先創建這個? –

回答

1

首先感謝回購。問題是你指定的路徑。
當您使用await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);時,這意味着您要將選定的數據複製到本地文件夾(不在數據庫文件夾內)以將其複製到數據庫文件夾中,您需要先創建一個數據庫文件夾,然後將該文件複製到該文件夾​​中。
現在我正在給你的AppDBPath修改錯誤的簡單方法。此解決方案僅將數據庫複製到您的本地文件夾而不是數據庫文件夾內

public const string AppDBPath = @"ParkingZoneDatabase.db"; 
public const string PackageDBPath = @"Databases\ParkingZoneDatabase.db"; 


     /// <summary> 
     /// Load SQL_LiteTable from Solution 
     /// </summary> 
     /// <param name="DBPATH"></param> 
     /// <returns></returns> 
     public async Task<bool> Init() 
     { 


      bool isDatabaseExisting = false; 

      try 
      { 
       StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(AppDBPath); 
       isDatabaseExisting = true; 
      } 
      catch 
      { 
       isDatabaseExisting = false; 
      } 


       if (!isDatabaseExisting) 
       { 

        StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync(PackageDBPath); 
        await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder); 
       } 


      return true;   
     } 

由於您使用AppDBPath作爲參考的代碼的數據庫其餘部分應該很好地工作。

+0

就是這樣!我之前實際上已經改變了PackageDBPath,因爲我認爲它是失敗的軟件包的複製代碼。但正如你解釋它是應用程序巴。 –