Cannot delete sqlite file from code using Python Qt for Qgis plug-in

المشرف العام

Administrator
طاقم الإدارة
I am producing a Qgis plugin using Qt in Python and the QtSql database module,I am using sqlite and running on a Windows 7 64bit .Each time the user stops the application and does not want to keep changes to the db in the last session the current copy of the db is deletedand a backup copy saved at the beginning of the session is renamed and restored as the main plug-in database.Qt and QGis does not allow me to complete this action which I am trying to achieve with two strategies

Strategy 1: simply let the code delete the sqlite file and rename it:

if click_btn == QMessageBox.No: os.remove(self.parser.params['RNDatastore']) # contains the path to the db os.rename(dir_path + "\\roadnet{0}.sqlite".format(0), self.parser.params['RNDatastore'])I get an [Error 32] from Windows (file is opened and used by another process) because the file is opened by Qgis, even if is not used.

Strategy 2: closing the db connection delete the file and at the following application start (the application class is not deleted) re-establishing the connection and re-open the connection again:

# here I close the connectionif click_btn == QMessageBox.No: self.db_con.close_db_connection() os.remove(self.parser.params['RNDatastore']) os.rename(dir_path + "\\roadnet{0}.sqlite".format(0), self.parser.params['RNDatastore']# here I try to re-open the connection again if not self.db.isOpen(): # False: the connection is not open bacause is closed from the code above self.db.addDatabase("QSPATIALITE") self.db.setDatabaseName(os.path.join(self.plugin_dir, "roadnet.sqlite")) self.db.open() print str(self.plugin_dir) print "last error is " + self.db.lastError().text() # Driver is not loaded print "db is valid " + str(self.db.isValid()) # False print "db is open 2 " + str(self.db.isOpen()) # FalseIn this case I am able to close the db connection and delete the file but I am not able to re-open it again, and consequently I can't access the db for whatever operation unless I reload the plug-in. If I check drivers availability true is returned but the addDatabase function fails to load the driver.

Things I also tried to do:

  • open the .sqite file in r mode and closing it and then try to delete it: --result-- failed because Qgis, or some other process I am notaware of, keeps it open.
  • open the .sqlite file in w+ mode and close it and try to delete it: --result-- failed the file content is deleted but the file is not deleted and I get the above Windows error.
    • delete the db connection class and re-instantiate it again: --result-- still does not seem to load the driver.
    • check if I had the .sqlite or file explorer opened somewhere else in Windows : --result-- negative, it seems that the file is not opened apart from the QGis process, but I might be wrong.
What I would need is either a solution to force the process that holds the file opened to release the lock or a solution to make the QsqlDatabase object load the driver.



أكثر...
 
أعلى