2

当我尝试在 WinRT 中 SQLite 的同一个事务中对同一个表进行多次更新时,我得到一个“无法打开”的异常。

我为此用例创建了一个示例应用程序。下面的代码在单击第一个按钮时,我在事务中创建一个表,在单击另一个按钮时,我试图再次更新同一记录。在那里,它会引发“无法打开”的异常。

应用代码

private SQLiteConnection getConnection()
{
    var connection = SQLiteConnectionPool.Shared.GetConnection(
                           new SQLiteConnectionString("sample.db", false));
    return connection;
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    SQLiteConnection con = getConnection();
    con.BeginTransaction();
    {
        try
        {
            var command = new SQLiteCommand(con) {
                CommandText =
                    @"create table konysyncMETAINFO (
                        id bigint not null,
                        versionnumber int,
                        lastserversynccontext nvarchar(1000),
                        filtervalue nvarchar(1000),
                        replaysequencenumber integer,
                        lastgeneratedid integer,
                        scopename nvarchar(100),
                        primary key (id))"
            };
            var xyz = (double)command.ExecuteNonQuery();

            var command2 = new SQLiteCommand(con) {
                CommandText =
                    @"insert into konysyncMETAINFO
                        (id,scopename,versionnumber,lastserversynccontext,
                         replaysequencenumber,lastgeneratedid)
                      values ('1','testscope','0','','0','-1')"
            };
            var xyz2 = (double)command2.ExecuteNonQuery();

            var command3 = new SQLiteCommand(con) {
                CommandText =
                    @"insert into konysyncMETAINFO
                        (id,scopename,versionnumber,lastserversynccontext,
                         replaysequencenumber,lastgeneratedid)
                      values ('2','testscope2','0','','0','-1')"
            };
            var xyz3 = (double)command3.ExecuteNonQuery();
            con.Commit();
        }
        catch (Exception ex)
        {
            con.Rollback();
        }
    }
}

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    SQLiteConnection con = getConnection();
    con.BeginTransaction();
    {
        try
        {
            var command = new SQLiteCommand(con) {
                CommandText =
                    @"Update konysyncMETAINFO
                      set lastgeneratedid='4'
                      WHERE scopename = 'testscope'"
            };
            var xyz = (double)command.ExecuteNonQuery();

            //var command2 = new SQLiteCommand(con) { CommandText = "insert into konysyncMETAINFO (id,scopename,versionnumber,lastserversynccontext,replaysequencenumber,lastgeneratedid) values ('3','testscope3','0','','0','-1')" };
            //var xyz2 = (double)command2.ExecuteNonQuery();

            var command3 = new SQLiteCommand(con) {
                CommandText =
                    @"Update konysyncMETAINFO 
                      set lastgeneratedid='3'
                      WHERE scopename = 'testscope'"
            };
            var xyz3 = (double)command3.ExecuteNonQuery();
            con.Commit();
        }
        catch (Exception ex)
        {
            con.Rollback();
        }
    }
}
4

2 回答 2

3

您可能在 sqlite-net 中偶然发现了与我相同的错误。我创建了一个修复程序,该修复程序已被拉回主分支,但此后NuGet 上没有新版本。您可以直接下载最新的资源并检查它是否可以解决您的问题。

于 2012-12-04T17:29:52.680 回答
0

在打开数据库后立即添加此代码将解决问题(无论是在侧载时还是在应用程序通过商店下载时)

verifySQLResult(SQLite3.Execute(_db, "PRAGMA temp_store = memory;", 0, 0, 0));

来源:

https://github.com/praeclarum/sqlite-net/issues/78

于 2014-01-25T06:13:01.580 回答