- Home /
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[i];
}
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.
If you use the sqlite command line app, are you able to edit the database directly. I mean, is this uniquely a problem when you connect from script code. Unity itself doesn't know what a sqlite db is. I'd assume it's a permissions thing on the db.
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().
Answer by Jamora · Jul 25, 2013 at 09:04 AM
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.
}
Thanks, that worked perfectly. I also found that running as ad$$anonymous$$ solves it temporarily, but changing my script to that worked better. Thanks!
Answer by jXD · Jul 24, 2013 at 11:40 PM
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().
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
how to make the build in android which can access the local database sqlite 0 Answers
Game Database. SQLite, XML, JSON or PlayerPrefs? 1 Answer
database visualization 1 Answer
SqliteException: Unable to open the database file - Ready-made DB Provided To Access 0 Answers