D
D
Dmitry Fondomakin2018-07-18 16:22:21
SQLite
Dmitry Fondomakin, 2018-07-18 16:22:21

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("Жопа какая то!");
                    }
                }
            }
        }

Already tried on everyone, MSDN rummaged all. Cyberforum - similarly. There are not so many solutions on Google on this issue. Tried with transactions. Tried with a common single connection for the entire project. Reinstalled the SQLite driver. Recreated a DB and separate tables.
Moreover, through the shell, this request (INSERT) is executed without problems and any unlimited number of times, without a noticeable database lock (a few milliseconds, as it should be).
Request one. There are no cycles.
I already have thoughts too. Help...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Fondomakin, 2018-07-20
@defond

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

must read:
public static void Close()
        {
            connection.Close();
        }

Obvious typo, but suddenly someone will copy. )
Just in case, I post the correct code example:
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 question

Ask a Question

731 491 924 answers to any question