S
S
Staspost2020-04-22 19:25:54
SQLite
Staspost, 2020-04-22 19:25:54

How to optimize work with the database when the error “The database file is locked” occurs?

Good afternoon!
When I started writing the project, I thought I would limit myself to a couple of connections to the SQLite database - load and save the settings, so I wrote connecting and closing the connection in a simplified way, right in the methods. Then the project grew and I thought "Well, okay, one more method, don't rewrite because of it..." ))
But such "well, one more" methods turned out to be as a result of dozens in one class, as a result, they seem to interfere each other during work and still have to rewrite.))
Yesterday the first bell rang - the project hung and gave the error "The database file is locked" on the next method.
I wrapped the call to the database in Invoke and everything worked. Today, a few more methods have been added, and each of them produces the same error.
The methods look something like this:

private void clearActive()
    {
        try
        {
            path = Application.dataPath + "/StreamingAssets/db.bytes";
            scriptConnection.dbconnection = new SqliteConnection("URI=file:" + path);
            scriptConnection.dbconnection.Open();
            if (scriptConnection.dbconnection.State == ConnectionState.Open)
            {
                scriptConnection.cmd = new SqliteCommand();
                scriptConnection.cmd.Connection = scriptConnection.dbconnection;
                scriptConnection.cmd.CommandText = "UPDATE progress SET active = 0 WHERE active = 1";
                scriptConnection.cmd.ExecuteNonQuery();
                scriptConnection.dbconnection.Close();               
            }
            else
            {
                Debug.Log("Error connection");
            }
        }
        catch (Exception ex)
        {
            Debug.Log(ex);
        }
    }

The question is, what is the best way to optimize it? Transfer the connection to a separate static class (have not tried to do this yet)? May be something else?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2020-04-22
@staspost

Read about Lock.
Here
And Here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question