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. Monitoring an SQLite database with QFileSystemWatcher does not work as expected
Forum Updated to NodeBB v4.3 + New Features

Monitoring an SQLite database with QFileSystemWatcher does not work as expected

Scheduled Pinned Locked Moved Solved General and Desktop
28 Posts 5 Posters 4.7k Views 2 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #16

    Just throwing an idea out there: Qt Creator does this for source files so you should be able to check the source of Qt Creator and find the perfect solution to your problem

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    1 Reply Last reply
    3
    • l3u_L l3u_

      @JonB said in Monitoring an SQLite database with QFileSystemWatcher does not work as expected:

      @l3u_
      I don't really know what you expect to achieve realistically.

      The idea is quite simple. The database is not intended to be accessed by multiple processes. So if another process changes something while the program runs, it's possible that it breaks. By setting "locking_mode", I can prevent such changes, as simply the database can't be used by any other connection until it's closed. That's one thing I wanted (and achieved in an elegant way I think, as both my program does handle such a lock now, and e. g. a manual cli connection also refuses to work on the database as long as it's open).

      The other thing is to inform the user if e. g. the file has been accidentally deleted while it's open. By now, the program doesn't know it, and on the next change, all data is gone without an error message, which is imo not a desirable behavior. Instead, an error should pop up saying something like "The database file has been deleted, so we reset our program as if we closed it now".

      And this is probably possible by monitoring it via a QFileSystemWatcher, isn't it? If I check if it's still there on each change, I know if it was deleted and can handle this.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #17

      @l3u_
      As I tried to show in examples, I do not believe you can reliably achieve what you would like to achieve, at least not cross-platform.

      l3u_L 1 Reply Last reply
      0
      • l3u_L Offline
        l3u_L Offline
        l3u_
        wrote on last edited by
        #18

        As far as I can see, they also use a QFileSystemWatcher to handle this kind of changes.

        1 Reply Last reply
        0
        • JonBJ JonB

          @l3u_
          As I tried to show in examples, I do not believe you can reliably achieve what you would like to achieve, at least not cross-platform.

          l3u_L Offline
          l3u_L Offline
          l3u_
          wrote on last edited by l3u_
          #19

          @JonB Apart from the change protection (changes by accident by opening the same database with two program instances), I'm actually only interested in knowing if the file has been deleted during runtime … Should I simply let a QFileSystemWatcher watch it an check it's still there after each change? Or is this the wrong way?

          JonBJ 1 Reply Last reply
          0
          • l3u_L l3u_

            @JonB Apart from the change protection (changes by accident by opening the same database with two program instances), I'm actually only interested in knowing if the file has been deleted during runtime … Should I simply let a QFileSystemWatcher watch it an check it's still there after each change? Or is this the wrong way?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #20

            @l3u_
            I have said, I'm not convinced it will work in this circumstance, at least under Linux, and I still don't think you've said which OS you're under. But why don't you first test the situation: run up your database Qt app, then go separately try to delete the file and see whether you can? If you can, test to see if QFileSystemWatcher detects it correctly.

            I don't think you "let a QFileSystemWatcher watch it an check it's still there after each change". You set up a QFileSystemWatcher, then it will signal you on file delete. Rather than you do any checking at certain periods yourself.

            l3u_L 1 Reply Last reply
            0
            • JonBJ JonB

              @l3u_
              I have said, I'm not convinced it will work in this circumstance, at least under Linux, and I still don't think you've said which OS you're under. But why don't you first test the situation: run up your database Qt app, then go separately try to delete the file and see whether you can? If you can, test to see if QFileSystemWatcher detects it correctly.

              I don't think you "let a QFileSystemWatcher watch it an check it's still there after each change". You set up a QFileSystemWatcher, then it will signal you on file delete. Rather than you do any checking at certain periods yourself.

              l3u_L Offline
              l3u_L Offline
              l3u_
              wrote on last edited by
              #21

              @JonB I'm on Linux and also compile the program on Windows. And, at least on Linux, I can delete the file while it's open.

              The QFileSystemWatcher won't only signal me on file delete, but on each change, won't it? So I will have to find out if the file has been deleted on each change, won't I?

              JonBJ 1 Reply Last reply
              0
              • l3u_L l3u_

                @JonB I'm on Linux and also compile the program on Windows. And, at least on Linux, I can delete the file while it's open.

                The QFileSystemWatcher won't only signal me on file delete, but on each change, won't it? So I will have to find out if the file has been deleted on each change, won't I?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #22

                @l3u_

                I'm on Linux and also compile the program on Windows. And, at least on Linux, I can delete the file while it's open.

                I expected that. But I'm saying: in that situation, have you actually tested whether your QFileSystemWatcher signals you that's it's been deleted (while you still have the database open)? I'm not sure it will, you need to check.

                So I will have to find out if the file has been deleted on each change, won't I?

                I had thought you made "on each change you make to the database". Now I think you mean "on each change signalled by QFileSystemWatcher".

                void QFileSystemWatcher::fileChanged(const QString &path)¶
                This signal is emitted when the file at the specified path is modified, renamed or removed from disk.

                Yes, that's all you get. So you would have to check whether the file still exists. Don't do that too often!

                Worse, I presume this means you'll get signalled when you make changes to your own database file.

                Like I said, you now need to try a bit of code to see when you do & do not get these file notification messages.

                l3u_L 1 Reply Last reply
                0
                • JonBJ JonB

                  @l3u_

                  I'm on Linux and also compile the program on Windows. And, at least on Linux, I can delete the file while it's open.

                  I expected that. But I'm saying: in that situation, have you actually tested whether your QFileSystemWatcher signals you that's it's been deleted (while you still have the database open)? I'm not sure it will, you need to check.

                  So I will have to find out if the file has been deleted on each change, won't I?

                  I had thought you made "on each change you make to the database". Now I think you mean "on each change signalled by QFileSystemWatcher".

                  void QFileSystemWatcher::fileChanged(const QString &path)¶
                  This signal is emitted when the file at the specified path is modified, renamed or removed from disk.

                  Yes, that's all you get. So you would have to check whether the file still exists. Don't do that too often!

                  Worse, I presume this means you'll get signalled when you make changes to your own database file.

                  Like I said, you now need to try a bit of code to see when you do & do not get these file notification messages.

                  l3u_L Offline
                  l3u_L Offline
                  l3u_
                  wrote on last edited by
                  #23

                  @JonB said in Monitoring an SQLite database with QFileSystemWatcher does not work as expected:

                  Worse, I presume this means you'll get signalled when you make changes to your own database file.

                  Yes, and that's the problem – I already tried to setup a QFileSystemWatcher and silent it when I do changes myself (because I'm only interested in changes done outside the scope of my program cf. my initial post!), but that didn't work as expected.

                  And now I'm, not sure if using it is the right way for a case that won't happen very often. I mean if e. g. KWrite sets up a QFileSystemWatcher, the only case it will receive a signal is when something else modified or deleted the file, because it lives and is modified in KWrite's cache, not on disk. But I work on the database directly, so it's changed with each INSERT or UPDATE statement …

                  JonBJ 1 Reply Last reply
                  0
                  • l3u_L l3u_

                    @JonB said in Monitoring an SQLite database with QFileSystemWatcher does not work as expected:

                    Worse, I presume this means you'll get signalled when you make changes to your own database file.

                    Yes, and that's the problem – I already tried to setup a QFileSystemWatcher and silent it when I do changes myself (because I'm only interested in changes done outside the scope of my program cf. my initial post!), but that didn't work as expected.

                    And now I'm, not sure if using it is the right way for a case that won't happen very often. I mean if e. g. KWrite sets up a QFileSystemWatcher, the only case it will receive a signal is when something else modified or deleted the file, because it lives and is modified in KWrite's cache, not on disk. But I work on the database directly, so it's changed with each INSERT or UPDATE statement …

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #24

                    @l3u_
                    Which is why I wouldn't be doing any of this file watching stuff.

                    l3u_L 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @l3u_
                      Which is why I wouldn't be doing any of this file watching stuff.

                      l3u_L Offline
                      l3u_L Offline
                      l3u_
                      wrote on last edited by
                      #25

                      @JonB Most probably, it's simply the wrong approach at all when using an SQLite database …

                      Perhaps, I should create a copy of the original database in a temporary location, work on this one and copy it back if the user "saves".

                      Apparently, it's also possible to read an on-disk database to a :memory: database and back using SQLite's backup mechanism … this way, one could work on in-memory data without having to mess with the user or other processes changing the data. But this doesn't seem to be too easy, at least, it's not supported by Qt functions.

                      Well, let's see if and how this can be solved …

                      JonBJ 1 Reply Last reply
                      0
                      • l3u_L l3u_

                        @JonB Most probably, it's simply the wrong approach at all when using an SQLite database …

                        Perhaps, I should create a copy of the original database in a temporary location, work on this one and copy it back if the user "saves".

                        Apparently, it's also possible to read an on-disk database to a :memory: database and back using SQLite's backup mechanism … this way, one could work on in-memory data without having to mess with the user or other processes changing the data. But this doesn't seem to be too easy, at least, it's not supported by Qt functions.

                        Well, let's see if and how this can be solved …

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #26

                        @l3u_
                        This is all possible.

                        But why is your situation so special? Many programs will "not do well" if something external deletes whatever files they need. Why isn't the world full of code for dealing with what if someone deletes a database you're using? Have you asked this as a non-Qt question on stackoverflow? That's how I see it.

                        l3u_L 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @l3u_
                          This is all possible.

                          But why is your situation so special? Many programs will "not do well" if something external deletes whatever files they need. Why isn't the world full of code for dealing with what if someone deletes a database you're using? Have you asked this as a non-Qt question on stackoverflow? That's how I see it.

                          l3u_L Offline
                          l3u_L Offline
                          l3u_
                          wrote on last edited by
                          #27

                          @JonB It's not speacial at all I think – I thought I could make my program a bit more fool-proof without much effort, but perhaps, I simply should leave it as-is, as nobody can expect it to work normally if an open database is manipulated or deleted – and if somebody does so, he or she will know what he or she does ;-)

                          JonBJ 1 Reply Last reply
                          0
                          • l3u_L l3u_

                            @JonB It's not speacial at all I think – I thought I could make my program a bit more fool-proof without much effort, but perhaps, I simply should leave it as-is, as nobody can expect it to work normally if an open database is manipulated or deleted – and if somebody does so, he or she will know what he or she does ;-)

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #28

                            @l3u_ That would be my approach, unless you have some special case. :)

                            1 Reply Last reply
                            0

                            • Login

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