多个更新语句抛出“无法打开”

时间:2012-12-04 14:39:34

标签: c# sqlite windows-runtime sql-update sqlite-net

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

我为此用例创建了一个示例应用程序。下面是代码,其中单击第一个按钮,我在事务中创建一个表,并在单击另一个按钮,我试图第二次更新相同的记录。在那里,它抛出了“无法打开”的例外。

app code

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();
        }
    }
}

2 个答案:

答案 0 :(得分:3)

你可能偶然发现了我在sqlite-net中的the same bug。我已经创建了a fix,它已被拉回到主叉中,但从那时起就没有新的release on NuGet。你可以download the latest sources directly并检查它是否解决了你的问题。

答案 1 :(得分:0)

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

  

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

源:

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