创建SQLite数据库时出现异常

时间:2015-01-06 17:22:33

标签: c# sqlite exception windows-phone windows-phone-8.1

我开发了一个使用本地SQLite数据库的通用应用程序。由于我需要知道数据库版本,因此我会存储包含此信息的其他信息。

在应用程序启动时,我检查数据库是否存在,以及数据库版本是否已更改。

public sealed partial class App : Application
{
    // DB Name
    public static string DBFileName = "myDb.sqlite";
    public static string DBpath;

    // DB Version
    public static string DBVersionFilename = "myDb.version";
    public static string DBVersionPath;
    public static string DBVersion = "1.1.0";

    public App()
    {
        this.InitializeComponent();
        this.Suspending += this.OnSuspending;
        ManageDB();
    }

    private async Task ManageDB()
    {
        if (!CheckDbExist().Result)
        {
            CreateDb();
        }
        else
        {
            if (CheckDbVersionUpdated().Result)
            {
                await DeleteDb();
                CreateDb();
            }
        }
    }

    private async Task<bool> CheckDbExist()
    {
        try
        {
            StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(DBFileName);
            DBpath = storageFile.Path;
            return true;
        }
        catch { }
        return false;
    }

    private void CreateDb()
    {
        try
        {
            StorageFile storageFile = ApplicationData.Current.LocalFolder.CreateFileAsync(DBFileName, CreationCollisionOption.OpenIfExists).GetResults();
            DBpath = storageFile.Path;
            var dbconnection = new SQLiteAsyncConnection(DBpath, false);
            dbconnection.CreateTableAsync<Article>();
            //...

            StorageFile file = ApplicationData.Current.LocalFolder.CreateFileAsync(DBVersionFilename, CreationCollisionOption.ReplaceExisting).GetResults();
            FileIO.WriteTextAsync(file, DBVersion);
        }
        catch (Exception e)
        { }
    }

    private async Task<bool> DeleteDb()
    {
        try 
        {
            StorageFile storageFile = ApplicationData.Current.LocalFolder.CreateFileAsync(DBFileName, CreationCollisionOption.OpenIfExists).GetResults();
            await storageFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
            return true;
        }
        catch { }
        return false;
    }

    private async Task<bool> CheckDbVersionUpdated()
    {
        try
        {
            string userDBVersion;
            StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(DBVersionFilename);
            userDBVersion = await FileIO.ReadTextAsync(file);
            if (userDBVersion != DBVersion)
                return true;
            else
                return false;
        }
        catch { }
        return false;
    }
}

CreateDB()

出现问题
  • 如果我一步一步地使用断点浏览此代码,则没有问题
  • 但如果我没有放置断点,我会在致电var dbconnection = new SQLiteAsyncConnection(DBpath, false);后遇到异常:
System.InvalidOperationException:在意外时间调用了一个方法。 在意外的时间调用了一个方法。    在Windows.Foundation.IAsyncOperation`1.GetResults()    在TestRating.App.CreateDb()}

您对所遇到的问题有所了解吗?还有另一种方法吗?

1 个答案:

答案 0 :(得分:-1)

当我删除“ .Results()”时,似乎“ CreateDb()”功能正常工作:

private async Task<bool> CreateDb()
{
    try
    {
        StorageFile storageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(DBFileName, CreationCollisionOption.OpenIfExists);
        DBpath = storageFile.Path;
        var dbconnection = new SQLiteAsyncConnection(DBpath, false);
        dbconnection.CreateTableAsync<Article>();
        //...
        StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(DBVersionFilename, CreationCollisionOption.ReplaceExisting);
        FileIO.WriteTextAsync(file, DBVersion);
        return true;
    }
    catch (Exception e)
    { }
    return false;
}

private async Task<bool> CreateDb() { try { StorageFile storageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(DBFileName, CreationCollisionOption.OpenIfExists); DBpath = storageFile.Path; var dbconnection = new SQLiteAsyncConnection(DBpath, false); dbconnection.CreateTableAsync<Article>(); //... StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(DBVersionFilename, CreationCollisionOption.ReplaceExisting); FileIO.WriteTextAsync(file, DBVersion); return true; } catch (Exception e) { } return false; }

你有解释吗?