SOLVED [SQLite] INSERT locks the database, how can I solve this problem?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
5 views
Hello

There was an incomprehensible for me. An ordinary SQLite database. Insert work fine. Five days ago appeared a strange trend - after rebooting the PC database allows you to do 1-2 inserts, after which any attempts result in an error "database is locked".

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 + "')"; // where new_guid new GUID // Perem.id_anketa room // guid_LMSZ - GUID from another table using (SQLiteTransaction dbTrans = connection.BeginTransaction()) { using (SQLiteCommand cmd = connection.CreateCommand()) { cmd.CommandText = Zapros; cmd.ExecuteNonQuery(); } try { dbTrans.Commit(); } catch { MessageBox.Show("this is fucked!"); } } } }


Already tried everything, searched through the entire MSDN. Cyberforum - the same. Google solutions is not so much on this issue. Tried transactions. Tried with common single connection for the whole project. Re-installed the SQLite driver. Recreated the DB and separate tables.

Moreover, through shell this query (INSERT) is executed without problems and any unlimited number of times without appreciable blocking of the database (a few milliseconds, as it should be).

Request single. No cycles no.

Thoughts I have too. Help...
by | 5 views

1 Answer

0 like 0 dislike
Helped me to solve the issue on Cyber forum.

I don't understand how, because I was logically same class. Called differently, and query building was closer to the recommendations of the MSDN, but the fact remains - the code in the answer works, mine doesn't.

And addition to the answer, instead of:
public static Close() { connection.Close(); }

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

Obvious typo, but suddenly someone will copy. )

Just in case, place the correct sample code:
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(); } // you can also add methods to retrieve the data when needed... 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("what bad Luck!"); } } } }
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
asked Mar 21, 2019 by v1z
0 like 0 dislike
2 answers
110,608 questions
257,186 answers
0 comments
28,014 users