【发布时间】:2018-04-27 20:11:11
【问题描述】:
我正在尝试将预加载的 SQLite 数据库复制到我的 UWP 应用程序中。在初始安装时,它会复制“test.db”,但大小为 0 字节,并且没有表或数据。原始数据库为 1300 字节,包含数据和表格。
另一个事实...当我使用 Visual Studio 2017 创建应用程序并编译和运行/调试应用程序时,它工作正常,但是当我旁加载 appx 文件或从 Windows 应用商店下载时,数据库为空。
这是我正在使用的代码:
Task task = CopyDatabase();
private async Task CopyDatabase()
{
bool isDatabaseExisting = false;
try
{
StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync("Express.db");
isDatabaseExisting = true;
}
catch
{
isDatabaseExisting = false;
}
if (!isDatabaseExisting)
{
StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync("Express.db");
await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder, "Express.db", NameCollisionOption.ReplaceExisting);
}
}
我没有收到任何错误消息。
【问题讨论】:
-
我无法回答我的头顶但有一些事情要尝试:分配“databaseFile”后创建一个 StorageFile 选项对象并在运行时使用断点检查文件大小。看到它是 0 或 1300 字节。另外,不要将其复制到 LocalFolder,而是尝试在 LocalFolder 中创建一个子文件夹并将其复制到那里。
-
您何时调用
CopyDatabase方法?在“App.xaml.cs”OnLaunched处理程序中? -
我在 public MainPage() public MainPage() { gui = this; 中调用“CopyDatabase”初始化组件();任务task = CopyDatabase();数据设置(); CreateNewChartButton.Visibility = Visibility.Collapsed; SignInButton_Click(null, null); }
标签: sqlite uwp windows-store-apps