Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Deleting local sqlite-file not possible: Access denied
Forum Updated to NodeBB v4.3 + New Features

Deleting local sqlite-file not possible: Access denied

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 4 Posters 6.3k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #10

    Can you share the code of your minimal sample project ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    1
    • randsfjordenR Offline
      randsfjordenR Offline
      randsfjorden
      wrote on last edited by randsfjorden
      #11

      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?

      1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #12

        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.

        1 Reply Last reply
        1
        • randsfjordenR Offline
          randsfjordenR Offline
          randsfjorden
          wrote on last edited by randsfjorden
          #13

          The Test System is Windows 10, and I am indeed logged on as Lukas (who is an Administrator).

          mrjjM 1 Reply Last reply
          1
          • randsfjordenR randsfjorden

            The Test System is Windows 10, and I am indeed logged on as Lukas (who is an Administrator).

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #14

            @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?

            1 Reply Last reply
            1
            • randsfjordenR Offline
              randsfjordenR Offline
              randsfjorden
              wrote on last edited by
              #15

              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).

              1 Reply Last reply
              1
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #16

                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 :)

                1 Reply Last reply
                1
                • randsfjordenR Offline
                  randsfjordenR Offline
                  randsfjorden
                  wrote on last edited by randsfjorden
                  #17

                  Here I uploaded the zipped project https://1drv.ms/u/s!ApYSo539xqGngcF-QFMUiHaL6sQVdQ

                  mrjjM 1 Reply Last reply
                  1
                  • randsfjordenR randsfjorden

                    Here I uploaded the zipped project https://1drv.ms/u/s!ApYSo539xqGngcF-QFMUiHaL6sQVdQ

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #18

                    @randsfjorden

                    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.
                    alt text

                    If i uncheck readonly from file. f.remove() works.

                    1 Reply Last reply
                    2
                    • randsfjordenR Offline
                      randsfjordenR Offline
                      randsfjorden
                      wrote on last edited by randsfjorden
                      #19

                      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 :)

                      mrjjM 1 Reply Last reply
                      1
                      • randsfjordenR randsfjorden

                        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 :)

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #20

                        @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.

                        1 Reply Last reply
                        1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved