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. SQlite save database at powerfailure
Forum Update on Monday, May 27th 2025

SQlite save database at powerfailure

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 3.7k Views
  • 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.
  • M Offline
    M Offline
    M001
    wrote on 1 Oct 2013, 08:54 last edited by
    #1

    Hello,
    I am using Qt5.1 and SQlite. This is the configuration of my Database:
    @
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(path_database);
    QSqlQuery query;
    query.exec("PRAGMA page_size = 4096;");
    query.exec("PRAGMA cache_size = 16384;");
    query.exec("PRAGMA temp_store = MEMORY;");
    query.exec("PRAGMA journal_mode = PERSIST;");
    query.exec("PRAGMA locking_mode = EXCLUSIVE;");
    query.exec("PRAGMA synchronous = OFF;");
    @

    everything works fine. I can write and read from the Database. But here's the problem, when I unplug the power and the System turns off hard sometimes the database is damaged. I write very often into the database so the possibility that there is an aktive writing command at the powerfailure is high. Im working on an windows embedet statdart 7 system. Is there any possibility to save the database from damage?

    Tank you for your help

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 1 Oct 2013, 09:35 last edited by
      #2

      AFAIK you can do nothing in case of an unplanned power outage. But when anything is being written into a database a record of the original unchanged database content is added to rollback journal. So when the system is back online it searches for any journal files and restores the database as per those files. See "RollBack Journal":http://sqlite.org/lockingv3.html and "http://www.sqlite.org/howtocorrupt.html":http://www.sqlite.org/howtocorrupt.html for more details

      157

      1 Reply Last reply
      0
      • M Offline
        M Offline
        M001
        wrote on 1 Oct 2013, 10:06 last edited by
        #3

        there is a journal file, but my software dosn't restore the database. Do i have to start a rollback by myself, or is this made automatically?

        1 Reply Last reply
        0
        • P Offline
          P Offline
          p3c0
          Moderators
          wrote on 1 Oct 2013, 10:16 last edited by
          #4

          From the SQLite docs "If the journal files are moved, deleted, or renamed after a crash or power failure, then automatic recovery will not work and the database may go corrupt.". What is the name of your journal file ? Also is there any particular reason you have "PRAGMA synchronous = OFF;" when executing query ?

          157

          1 Reply Last reply
          0
          • M Offline
            M Offline
            M001
            wrote on 1 Oct 2013, 10:36 last edited by
            #5

            the journal file is there:
            database.db
            database.db-journal

            I set PRAGMA synchronous = OFF because when I set it to NORMAL the database is way to slow for my application.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 1 Oct 2013, 10:46 last edited by
              #6

              Hi,

              If you really have that much power outage happening to your system or if you want it to be resistant to these, you should also have some sort of UPS that would allow it to shutdown gracefully. A standard system might recover from the occasional unplanned power off, but if it's happening regularly you might damage the filesystem and not only your database file.

              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
              0
              • P Offline
                P Offline
                p3c0
                Moderators
                wrote on 1 Oct 2013, 11:36 last edited by
                #7

                The Rollback happens automatically as soon as the DB is opened. But it will fail if the process can't lock the database. Is any other program accessing the database ? Check if the database is locked.

                157

                1 Reply Last reply
                0

                1/7

                1 Oct 2013, 08:54

                • Login

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