我有一个用 C# 编写的 Windows 10 UWP 应用程序。我正在使用 SQLite 在本地存储数据。我遇到的问题是文件永远不会使用此代码保存和/或检索。它应该可以工作,但我无法找出问题所在。
dbExists 总是评估为 false,那么我在这里错过了什么?
private SQLiteConnection localConn;
private string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "myDatabase.db");
public async void DBInit()
{
bool dbExists = false;
try
{
var store = await ApplicationData.Current.LocalFolder.GetFileAsync(dbPath);
dbExists = true;
}
catch { dbExists = false; }
if (!dbExists)
{
using (localConn = new SQLiteConnection(new SQLitePlatformWinRT(), dbPath))
{
// Create table
localConn.CreateTable<MyTable>();
}
}
else // CURRENTLY NOT FIRING!!
{}
}
最佳答案
请考虑使用以下代码来创建和访问数据库文件:
StorageFile notesFile = await storageFolder.CreateFileAsync(dbPath, CreationCollisionOption.OpenIfExists);
如果文件不存在,这将创建新文件,并在文件已创建时检索它。
请查看我的博客文章,了解有关 UWP 数据存储的更多信息: https://mobileprogrammerblog.wordpress.com/2016/05/23/universal-windows-10-apps-data-storage/
关于c# - SQLite 不存储/检索数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37731158/