2016-03-05 47 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,因为我认为它是失败的软件包的复制代码。但正如你解释它是应用程序巴。 –