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. QSqlDatabase - save/retrieve database to/from hard disk
Forum Updated to NodeBB v4.3 + New Features

QSqlDatabase - save/retrieve database to/from hard disk

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.9k Views 1 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.
  • S Offline
    S Offline
    simozz
    wrote on 5 Apr 2019, 09:53 last edited by
    #1

    Hi all,

    I want to gain some experience with QT and database so I am studyng the Books example

    https://doc.qt.io/qt-5.11/qtsql-books-example.html

    My question is: would it possible to store that current database to hard disk, and retrieve it later after a program restart ?
    If so, what QT's class shold I use or what is the preferred way to achieve this ?

    Thank you.
    Regards,
    s.

    J 1 Reply Last reply 5 Apr 2019, 09:56
    0
    • S simozz
      5 Apr 2019, 09:53

      Hi all,

      I want to gain some experience with QT and database so I am studyng the Books example

      https://doc.qt.io/qt-5.11/qtsql-books-example.html

      My question is: would it possible to store that current database to hard disk, and retrieve it later after a program restart ?
      If so, what QT's class shold I use or what is the preferred way to achieve this ?

      Thank you.
      Regards,
      s.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 5 Apr 2019, 09:56 last edited by
      #2

      @simozz Use SQLite as database for that - it is file based and supported by Qt.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 5 Apr 2019, 10:00 last edited by mrjj 4 May 2019, 10:01
        #3

        Hi
        To add to @jsulm
        The reason its not saved to disk with that example is that in
        books/initdb.h, function
        QSqlError initDb()
        {
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName(":memory:");

        its set to be in memory
        So you can change that to a file path and it will be stored on disk.

           db.setDatabaseName("c:\\somefolder\\dbname.db");
        
        1 Reply Last reply
        4
        • S Offline
          S Offline
          simozz
          wrote on 5 Apr 2019, 10:31 last edited by simozz 4 May 2019, 13:01
          #4

          Hello again and thank you for the quick replies !

          @jsulm Thanks, I will study better SQLite.

          @mrjj Good to know your suggestion.
          But what if the file already exists ?

          Do the function

          db.setDatabaseName("c:\\somefolder\\dbname.db");
          

          will overwrite the dbname.db content ?
          I suppose it will not and I will have to check for already existing records.
          Do you confirm ?

          Thanks.
          s

          M 1 Reply Last reply 5 Apr 2019, 11:22
          0
          • S simozz
            5 Apr 2019, 10:31

            Hello again and thank you for the quick replies !

            @jsulm Thanks, I will study better SQLite.

            @mrjj Good to know your suggestion.
            But what if the file already exists ?

            Do the function

            db.setDatabaseName("c:\\somefolder\\dbname.db");
            

            will overwrite the dbname.db content ?
            I suppose it will not and I will have to check for already existing records.
            Do you confirm ?

            Thanks.
            s

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 5 Apr 2019, 11:22 last edited by
            #5

            @simozz said in QSqlDatabase - save/retrieve database to/from hard disk:

            setDatabaseName

            Hi. it won't override as you would normally set it to open existing
            DB and only in case the file not exist, it will be created.
            Also, a DB can have multiple tables and hence if you want to clear all
            records, you have to do that manually with SQL.

            1 Reply Last reply
            2
            • S Offline
              S Offline
              simozz
              wrote on 5 Apr 2019, 12:47 last edited by
              #6

              Thank yoy @mrjj

              M 1 Reply Last reply 5 Apr 2019, 12:51
              0
              • S simozz
                5 Apr 2019, 12:47

                Thank yoy @mrjj

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 5 Apr 2019, 12:51 last edited by
                #7

                @simozz
                Hi
                Just a small note.
                You can use
                qApp->applicationDirPath();
                to get the path where exe is. Normally that is in the build folder. ( when you develop)
                So you can use this to have a path to the DB that is always valid.
                even on other machine.

                1 Reply Last reply
                2

                1/7

                5 Apr 2019, 09:53

                • 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