I get a “cannot open” exception when I try to make several updates in the same table in one transaction on SQLite in WinRT.
I created a sample application for this use case. Below is the code where, when I press the first button, I create a table in a transaction, and when I press another button, I try to update the same record a second time. There, it throws a “cannot open” exception.
application 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 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();
}
}
}