Good day I'm currenlty having a problem on my sqlite database in unity using c#.
So far whenever I tried to read my database it works perfectly fine.
But when I try to update it, it always gives me the "Database file is locked" even if there are no commands that are available yet or all commands are disposed and closed.
here is my code:
string conn = "URI=file:" + Application.dataPath + "/Database/SampleDB";
IDbConnection dbconn;
dbconn = (IDbConnection)new SqliteConnection (conn);
dbconn.Open ();
IDbCommand dbcmd = dbconn.CreateCommand();
string sqlQuery = "UPDATE UserScore SET Highscore = '1'";
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbconn.Close();
dbconn = null;
I use this code in both the reading and updating of the database. I also tried the "using" command here is the code for reading :
using (SqliteConnection c = new SqliteConnection(conn))
{
c.Open();
using (SqliteCommand cmd = new SqliteCommand(sqlQuery, c))
{
using (SqliteDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
currHighScore = rdr.GetInt32(0);
currHighDist = rdr.GetInt32(1);
Debug.Log( "High Score : " + currHighScore + ", High Distance: " + currHighDist);
}
}
}
}
and here is for updating:
using (SqliteConnection c = new SqliteConnection(conn2))
{
c.Open();
using (SqliteCommand cmd = new SqliteCommand(sqlQuery2, c))
{
if (MoveSanji.scoreAccumulate > currHighScore) {
cmd.ExecuteNonQuery(); //or cmd.ExecuteReader();
}
}
}
Aucun commentaire:
Enregistrer un commentaire