Hello, everyone.
I have some problems in c# SQLite data update and creating.
I'm using SQLite as a database in my c# windows form application.
By the way, sometimes I've got a "database is locked" error in SQLite, when I try to insert or update data.
While searching the way on google, I got know that it will be able to happen because multi-threads is working or the database is in processing.
But I can't find what thread or process hurt this database updating correctly.
I want to prevent the error and finally solve this problem asa.
Thanks for your time.
What I have tried:
static SQLiteConnection CreateConnection(string dbName)
{
SQLiteConnection sqlite_conn;
sqlite_conn = new SQLiteConnection("Data Source=" + dbName + ".db; PRAGMA journal_mode = WAL; Version = 3; New = True; Compress = True; Connection Timeout=0 ");
try
{
sqlite_conn.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
return sqlite_conn;
}
sqlite_conn = CreateConnection(constants.dbName);
InsertLog(sqlite_conn, 2, "xxx", "yyyyy");
public void InsertLog(SQLiteConnection conn, int logType, string logTitle, string logContent, string logContent_2 = "")
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
try
{
DateTime logDate = DateTime.Now;
SQLiteCommand sqlite_cmd;
string Createsql = "INSERT INTO " + constants.tbNames[14] + " (logType, logTitle, logContent, logContent_2, logDate) VALUES (@logType, @logTitle, @logContent, @logContent_2, @logDate)";
sqlite_cmd = conn.CreateCommand();
sqlite_cmd.CommandText = Createsql;
sqlite_cmd.Parameters.AddWithValue("@logType", logType);
sqlite_cmd.Parameters.AddWithValue("@logTitle", logTitle);
sqlite_cmd.Parameters.AddWithValue("@logContent", logContent);
sqlite_cmd.Parameters.AddWithValue("@logContent_2", logContent_2);
sqlite_cmd.Parameters.AddWithValue("@logDate", logDate);
sqlite_cmd.ExecuteNonQuery();
}
catch (Exception e)
{
conn.Close();
return;
}
conn.Close();
return;
}