SQLite 不存储/检索数据库 [英] SQLite not storing/retrieving database

查看:29
本文介绍了SQLite 不存储/检索数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 C# 编写的 Windows 10 UWP 应用程序.我正在使用 SQLite 在本地存储我的数据.我遇到的问题是从未使用此代码保存和/或检索文件.它应该可以工作,但我不知道出了什么问题.

I've got a Windows 10 UWP application written in C#. I'm using SQLite to store my data locally. The issue I'm experiencing is that the file is never saved and/or retrieved using this code. It should work, but I can't find out what's wrong.

dbExists 总是评估为 false,那么我在这里遗漏了什么?

dbExists always evaluates to false, so what am I missing here?

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!!
    {}
}

推荐答案

请考虑使用以下代码来创建和访问数据库文件:

Please consider using below code to create and access database file:

StorageFile notesFile = await storageFolder.CreateFileAsync(dbPath, CreationCollisionOption.OpenIfExists);

如果文件不存在,这将创建新文件,并在已创建时检索它.

This will create new file if it does not exists and retrieve it when it is already created.

请查看我的博客文章以了解有关 UWP 数据存储的更多信息:https://mobileprogrammerblog.wordpress.com/2016/05/23/universal-windows-10-apps-data-storage/

Please check my blog article to see more about UWP Data Storage: https://mobileprogrammerblog.wordpress.com/2016/05/23/universal-windows-10-apps-data-storage/

这篇关于SQLite 不存储/检索数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆