如何解决 SQLite 数据库的错误路径?
How to resolve an incorrect path to SQLite Database?
概述 - 我添加了一些代码以将现有数据库复制到设备的本地文件夹。到目前为止,第一个条件 if the existing db doesn't already exist 工作正常。
问题 -
但是当执行将现有数据库从解决方案文件夹复制到设备文件夹的代码行时,我收到 SQLite 错误。错误告诉我无法打开 db 文件。
在调试过程中,我发现 DBPath 与我的解决方案中的文件位置相同。所以我不太确定路径有什么问题。
(数据库作为内容附加并设置为"copy always"。)
包中db文件的完整路径是:
C:\Users\Brian\Documents\Visual Studio 2013\Projects\Parking Tag Picker WRT\Parking Tag Picker WRT\Databases\ParkingZoneDatabase.db
问题:如何将数据库路径解析为 SQLite 所需的正确路径class?
错误日志 - 这里抛出的异常的准确转储在 SQLite class:
中如下所示
SQLite.SQLiteException was unhandled by user code
HResult=-2146233088
Message=Could not open database file: C:\Data\Users\DefApps\APPDATA\Local\Packagesd00c25c-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 代码:(将现有数据库复制到设备上的本地文件夹的代码)
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;
}
我还检查了 db 文件本身的权限,没有 设置为只读 -
首先感谢您的回购。问题是您指定的路径。
当您使用 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
作为数据库的参考,其余代码应该可以正常工作。
概述 - 我添加了一些代码以将现有数据库复制到设备的本地文件夹。到目前为止,第一个条件 if the existing db doesn't already exist 工作正常。
问题 - 但是当执行将现有数据库从解决方案文件夹复制到设备文件夹的代码行时,我收到 SQLite 错误。错误告诉我无法打开 db 文件。
在调试过程中,我发现 DBPath 与我的解决方案中的文件位置相同。所以我不太确定路径有什么问题。
(数据库作为内容附加并设置为"copy always"。)
包中db文件的完整路径是:
C:\Users\Brian\Documents\Visual Studio 2013\Projects\Parking Tag Picker WRT\Parking Tag Picker WRT\Databases\ParkingZoneDatabase.db
问题:如何将数据库路径解析为 SQLite 所需的正确路径class?
错误日志 - 这里抛出的异常的准确转储在 SQLite class:
中如下所示SQLite.SQLiteException was unhandled by user code
HResult=-2146233088
Message=Could not open database file: C:\Data\Users\DefApps\APPDATA\Local\Packagesd00c25c-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 代码:(将现有数据库复制到设备上的本地文件夹的代码)
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;
}
我还检查了 db 文件本身的权限,没有 设置为只读 -
首先感谢您的回购。问题是您指定的路径。
当您使用 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
作为数据库的参考,其余代码应该可以正常工作。