Answer the question
In order to leave comments, you need to log in
SOLVED [SQLite] INSERT is blocking the database, how can I solve this issue?
Hello
, I have a strange situation. Normal SQLite DB. The insert worked fine. Five days ago, an incomprehensible trend appeared - after rebooting the PC, the database allows you to make 1-2 inserts, after which any attempts lead to the "DB is locked" error.
public static void ExecuteSqlTransaction(string Zapros)
{
string dbFileName = @"res\db\def.db";
using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + dbFileName + ";mode=Exclusive;datetimeformat=CurrentCulture"))
{
connection.Open();
//string Zapros = "insert into Def_LMSZ (guid, id_anketa, guid_msz) " +
// "values ('" + new_guid + "','" + Perem.id_anketa + "','" + guid_LMSZ + "')";
// где new_guid - новый гуид
// Perem.id_anketa - номер
// guid_LMSZ - гуид из другой таблицы
using (SQLiteTransaction dbTrans = connection.BeginTransaction())
{
using (SQLiteCommand cmd = connection.CreateCommand())
{
cmd.CommandText = Zapros;
cmd.ExecuteNonQuery();
}
try
{
dbTrans.Commit();
}
catch
{
MessageBox.Show("Жопа какая то!");
}
}
}
}
Answer the question
In order to leave comments, you need to log in
I was helped to resolve the issue on the Cyber forum.
I don't understand how, because I had the same class logically. It was called differently, and the construction of queries was closer to MSDN recommendations, but the fact remains - the code in the answer works, mine does not.
And an addition to the answer, instead of:
public static Close()
{
connection.Close();
}
public static void Close()
{
connection.Close();
}
class Database
{
const string dbFileName = @"res\db\def.db";
private static SQLiteConnection connection;
static Database()
{
connection = new SQLiteConnection("Data Source=" + dbFileName + ";mode=Exclusive;datetimeformat=CurrentCulture");
connection.Open();
}
public static void Close()
{
connection.Close();
}
// можно также добавить методы для получения данных при необходимости...
public static void ExecuteSqlTransaction(string query)
{
using (SQLiteTransaction dbTrans = connection.BeginTransaction())
{
using (SQLiteCommand cmd = connection.CreateCommand())
{
cmd.CommandText = query;
cmd.ExecuteNonQuery();
}
try
{
dbTrans.Commit();
}
catch
{
MessageBox.Show("Незадача какая то!");
}
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question