- Home /
Deleting the main database at runtime.
Helo. Iam making an installer/updater for my app that runs on android/standalone. In case of database update to a newer version failes I wont to make a backup of partiali updated database and than delete the original, to create a new database in it's place(to make new app usable to a customer until I manually fix his old database). But when trying to delete a database I get an exception that says that some app(my own) is holding the handle to that file and therefore I cannot delete it. Prior to try{DbDelete("sqliteDatabase.db")} I am calling a dispose dbConnection, reader and other IDisposables. Hre is some code:
private void DeleteOriginalDatabaseIfCopyExists(string databaseFullFilePath, string backedUpDatabaseFullPath)
{//DANGER
if (File.Exists( backedUpDatabaseFullPath))//if abckup was succsessfull...
{
Debug.Log("!!!!!!!!!!!!!!!!!!!!! BACKUP !!!!!!!!!!!!!!!!!!:" + backedUpDatabaseFullPath);
if (File.Exists(databaseFullFilePath))//...and original database stil exists
{
subsidiaryDatabaseInstance.CloseDB();
subsidiaryDatabaseInstance = null;
GC.Collect();
Debug.Log("!!!!!!!!!!!!!!!!!!!!! DELETING ORIGINAL DATABASE !!!!!!!!!!!!!!!!!!:"+ databaseFullFilePath);
try
{
File.Delete(databaseFullFilePath);
}
catch (Exception ex)
{
Debug.LogError("!!! Cannot delete the database couse some other program is holding the handle!!! "+ex);
}
}
}
}
public void CloseDB()
{
try
{
if (reader != null)
{
if (!reader.IsClosed)
reader.Close(); // this throws an exception
reader.Dispose();
reader = null;
}
}
catch (Exception ex) { Debug.Log("COULD NOT CLOSE reader.Dispose " + ex); }
try
{
if (dbCommand != null)
dbCommand.Dispose();
dbCommand = null;
}
catch (Exception ex) { Debug.Log("COULD NOT CLOSE dbcmd.Dispose " + ex); }
try
{
if (dbConnection != null)
{
dbConnection.Close();
dbConnection.Dispose();
dbConnection = null;
}
GC.Collect();
}
catch (Exception ex) { Debug.Log("COULD NOT CLOSE dbcon.Close " + ex); }
}
what do I do? Am I going wrong about ths?
Comment