Sqlite databases getting locked by Unity.

Hi guys,

Whenever I try to input values into my sqlite database from unity, it always throws an exception: SqliteException: The database file is locked
I still can read from it, but whenever I try to write into the db, Unity will hang for 10 secs and then that error pops up in the console. Here is my code:

public SqliteDataReader InsertInto(string tableName, string[] values)
	{
		string query = "INSERT INTO " + tableName + " VALUES(" + values[0];
		for (int i = 1; i < values.Length; ++i)
		{
			query += ", " + values*;*
  •  }*
    
  •  query += ")";*
    
  •  Debug.Log("Inserted!");*
    
  •  return ExecuteQuery(query);*
    
  • }*
    I also understand that this might occur due to the closing of connections being wrong(which I highly doubt so, but to be sure heres the closing code:
    public void CloseSqlConnection()
  • {*
  •  reader.Close();*
    
  •  reader = null;*
    
  •  dbCommand.Dispose();*
    
  •  dbCommand = null;*
    
  •  dbConnection.Close();*
    
  •  dbConnection = null;*
    
  •  GC.Collect();*
    
  •  Debug.Log("Disconnected from db.");*
    
  • }*
    This problem occurs even when I restart my PC, and launch Unity for the first time, so I doubt its the connection problem. Also, the database is in the proper folder, and it works fine(as I can read from it). It was working fine a few days back, and now it just decided to break.

I’m using the sqlite manager script from the unify wiki. I found the c# version on the unity forums some time back.
As for the closesqlconnection(), I’m using this from an editorwindow, so I call it every onlostfocus() and reconnect every onfocus().

I made a little test script to see how many times OnFocus is called when the window is opened the first time. Turns out it is called twice. So the reason why your database is locked for writing is because you have an open, possibly leaked connection to it. After the first time On(Lost)Focus behaves as expected.

I think something along these lines would work better:

[MenuItem("Item/Item")]
public static void Init(){
	GetWindow<TestScript>();
	//Initialize connection.
}

void OnFocus(){
	//If (connection == null)
	//Initialize connection.
}

void OnLostFocus(){
	//Close connection.
}