c# - SQLite 不存储/检索数据库

标签 c# windows sqlite uwp

我有一个用 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/

相关文章:

c# - 参数化查询需要参数 '@Id' ,但未提供

regex - Perl one liner corrupts file in Windows (carriage-return related issue)

java - 如何通过java在sqlite中插入日期

delphi - 将数据库复制到受限客户端

c# - 在 C# 4.0 中使用依赖注入(inject)时处理异常

c# - C# 中的静态派生类型?

c - "warning : No new line at end of file"是什么意思?

windows - 对 .bat 文件使用自定义 Tee 命令

sql - 如何在 Sqlite 中按周号分组并获取周号的开始日期和结束日期?

c# - 非管理员用户可以使用 Environment.MachineName 吗?