Deleting local sqlite-file not possible: Access denied
-
I'd close the database connection before removing it.
-
I tested it, and closing the connection before makes no difference, because it is closed automatically when it goes out of scope (this is at least what it sais in the documentation).
I have no more code than this (and no other connections), because i created a test-project in order to solve the problem, which doesn't contain anything else.
-
Can you share the code of your minimal sample project ?
-
I now found the error. It had nothing to do with the database itself.
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QFile> #include <QDir> #include <QDebug> #include <QStandardPaths> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QLatin1String("qrc:/main.qml"))); app.setApplicationName("sample"); app.setOrganizationName("developer"); QFileInfo databaseFile(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/database.txt"); QFile f(databaseFile.absoluteFilePath()); if(QDir().mkpath(databaseFile.absolutePath())); //the error is caused by the way I copied the database-file, and had nothing to do with the database-connection itself. //copying file from FileSystem if(!QFile::copy("D:/Dokumente/Qt/Workspace/test/" + databaseFile.fileName(), databaseFile.absoluteFilePath())) qWarning() << "Could not copy current version of database - check file permissions."; qDebug().noquote() << (f.remove() ? "Successfully deleted file" : "Could not delete old file." + f.errorString()); //when I create this way (copy the local file to the new destination), everything works as you would expect. //The file is deleted, so the environment is "clean" again for the way I copied the file that produced the Access-denied error. //copying file from Ressources if(!QFile::copy(":/" + databaseFile.fileName(), databaseFile.absoluteFilePath())) qWarning() << "Could not copy current version of database - check file permissions."; qDebug().noquote() << (f.remove() ? "Successfully deleted file" : "Could not delete old file. " + f.errorString()); //This does not work. The file is successfully copied, but after that it is imossible to delete the file through //Qt, even when the application is restarted or the whole Operating system is restarted it is impossible to delete the file. //The only way to resolve this is that I delete the file again myself. I have no clue why copying the file from the ressources creates that error. }
This is the simple code producing the access-denied error. The file type doesn't matter - as you can see I replaced the .sqlite file with a .txt-file.
The code produces the following output:
Successfully deleted file Could not delete old file. Access denied
Notice that copying works both times, because copying just prints a message if it fails.
The local file I use in the case when it works to delete it is the ressource file in my local filesystem. But why is it a problem to copy a file from the compiled-in ressources? -
Hi
what os are u on ?
Tried (hopefully) the same on win 10 and could not reproduce it.
I assume that u are in fact logged in as lukas on your test system. -
The Test System is Windows 10, and I am indeed logged on as Lukas (who is an Administrator).
-
@randsfjorden
Hmm. i copy a random file (from res) to a writable Location but i can always
remove it.
I wonder if it has to be a db or any file would do it? -
In my test case the file type doesn't matter - as you can see I now used a txt-file to produce the error.
The output and behavior is the same no matter which compiler I use (MinGW or MSVC2015).
-
Hi
is there anyway u could upload your complete sample for me ? ( google drive/ dropbox)
I feel i might do something wrong in order to reproduce it.
I have a feeling something is wrong if u cant even delete it after a reboot as any
locks will be removed so i wonder if Qt or local to ur system :) -
Here I uploaded the zipped project https://1drv.ms/u/s!ApYSo539xqGngcF-QFMUiHaL6sQVdQ
-
Hi
It seems that the "developer" folder that is created
is read only and hence the file is also made readonly and then remove wont delete it.
If i uncheck readonly from file. f.remove() works.
-
Thank you soooo much for your help and for your answer!
If I call f.setPermissions(QFile::ReadOther|QFile::WriteOther); before QFile::remove();, it removes the ReadOnly flag, and I may delete the file. I'm sorry that I didn't notice myself, and that it took you all so much time and 18 posts.
But it actually makes sense. Of course the compiled-in ressource file has the ReadOnly-flag set - nobody is supposed to change a compiled-in ressource, and it is not possible either. But I was simply not aware that the ReadOnly-flag also would be set for a file that was copied from ressources. Maybe this is something you could include in the documentation about QRessources? Would have helped me a lot :)
-
@randsfjorden
Well it was a tricky one as we all thought it was related to the db. :)Funny enough , its very much same situation as in old times when you copy files from a cdrom to some folder.
so maybe its not even really Qt related but an OS thing. I wonder if same would happen in linux.