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. what is the correct way to implement a Database class in a application
QtWS25 Last Chance

what is the correct way to implement a Database class in a application

Scheduled Pinned Locked Moved Solved General and Desktop
sqlitedatabasedesktopqt5.5
10 Posts 5 Posters 2.5k 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.
  • R Offline
    R Offline
    realhamidrezakp
    wrote on 4 Oct 2018, 10:08 last edited by
    #1

    Hi,
    I am new to QT and have some problem with implementing database.
    I have no idea how to communicate with database (SQLITE) correctly so that app wont get slow or crash.
    The app have a main window and user can open several text editors, each editor read a row of database and can save that row in another table of database.

    I think it is wrong if make a new connection to database for each operation. so i need some help about how to make connection with DB and how implement it so that app work as fast as possible.
    thanks for reading and answering.

    J K 2 Replies Last reply 4 Oct 2018, 10:32
    0
    • R realhamidrezakp
      4 Oct 2018, 10:08

      Hi,
      I am new to QT and have some problem with implementing database.
      I have no idea how to communicate with database (SQLITE) correctly so that app wont get slow or crash.
      The app have a main window and user can open several text editors, each editor read a row of database and can save that row in another table of database.

      I think it is wrong if make a new connection to database for each operation. so i need some help about how to make connection with DB and how implement it so that app work as fast as possible.
      thanks for reading and answering.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 4 Oct 2018, 10:32 last edited by
      #2

      @realhamidrezakp Did you read this: http://doc.qt.io/qt-5/sql-programming.html ?

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

      R 1 Reply Last reply 2 Dec 2018, 08:08
      5
      • R realhamidrezakp
        4 Oct 2018, 10:08

        Hi,
        I am new to QT and have some problem with implementing database.
        I have no idea how to communicate with database (SQLITE) correctly so that app wont get slow or crash.
        The app have a main window and user can open several text editors, each editor read a row of database and can save that row in another table of database.

        I think it is wrong if make a new connection to database for each operation. so i need some help about how to make connection with DB and how implement it so that app work as fast as possible.
        thanks for reading and answering.

        K Offline
        K Offline
        koahnig
        wrote on 4 Oct 2018, 10:36 last edited by
        #3

        @realhamidrezakp

        Hi and welcome to devnet forum

        There are a couple of SQL examples available with Qt. They shall give you a good start for your implementations. Note: the examples are programmed without external dependencies, therefore you will have some parts for your actual implementation.

        @realhamidrezakp said in what is the correct way to implement a Database class in a application:

        I think it is wrong if make a new connection to database for each operation. so i need some help about how to make connection with DB and how implement it so that app work as fast as possible.
        thanks for reading and answering.

        There is an example in the detailed description section on how to add and open an SQL db. Note: that the addDatabase is a static routine. You do not have to add and open the db with every statement. With the static routine it will be opened once and kept open until you decide to change this.

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        5
        • J jsulm
          4 Oct 2018, 10:32

          @realhamidrezakp Did you read this: http://doc.qt.io/qt-5/sql-programming.html ?

          R Offline
          R Offline
          realhamidrezakp
          wrote on 2 Dec 2018, 08:08 last edited by
          #4

          So sorry for late replay.
          @jsulm yes , but i cant understand some of it.
          @koahnig Thanks , i have read documents. but i cant use my database helper class. i get some errors and think i am using it incorrect .
          let me explain my program : i have a GUI app that can have multiple instance of other forms and each form can insert or retrieve data to/from database. therefore i need several functions for insert or retrieve data.one way is to open a database in my MainWindow and write functions in forms. but i want to use a class for my database and that class load or insert data itself and forms just call slots of class, i don't want to my database functions be all over my program :D .
          but i have no idea how to manipulate ui elements from database class the correct way . or maybe i am wrong and must write functions in each form.
          just let me know how you think i should implement database.

          K 1 Reply Last reply 2 Dec 2018, 10:43
          0
          • R realhamidrezakp
            2 Dec 2018, 08:08

            So sorry for late replay.
            @jsulm yes , but i cant understand some of it.
            @koahnig Thanks , i have read documents. but i cant use my database helper class. i get some errors and think i am using it incorrect .
            let me explain my program : i have a GUI app that can have multiple instance of other forms and each form can insert or retrieve data to/from database. therefore i need several functions for insert or retrieve data.one way is to open a database in my MainWindow and write functions in forms. but i want to use a class for my database and that class load or insert data itself and forms just call slots of class, i don't want to my database functions be all over my program :D .
            but i have no idea how to manipulate ui elements from database class the correct way . or maybe i am wrong and must write functions in each form.
            just let me know how you think i should implement database.

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 2 Dec 2018, 10:43 last edited by
            #5

            @realhamidrezakp said in what is the correct way to implement a Database class in a application:

            @jsulm yes , but i cant understand some of it.

            Which parts?

            @koahnig Thanks , i have read documents. but i cant use my database helper class. i get some errors and think i am using it incorrect .

            What errors do you get?

            i don't want to my database functions be all over my program :D .

            That's good thinking, but what is the exact problem?

            but i have no idea how to manipulate ui elements from database class the correct way.

            You don't. The same way as you pass data to the database class (i.e. with a signal/slot connection), it will notify the dependent class of changes - again with a signal.

            Read and abide by the Qt Code of Conduct

            R 1 Reply Last reply 3 Dec 2018, 12:33
            3
            • K kshegunov
              2 Dec 2018, 10:43

              @realhamidrezakp said in what is the correct way to implement a Database class in a application:

              @jsulm yes , but i cant understand some of it.

              Which parts?

              @koahnig Thanks , i have read documents. but i cant use my database helper class. i get some errors and think i am using it incorrect .

              What errors do you get?

              i don't want to my database functions be all over my program :D .

              That's good thinking, but what is the exact problem?

              but i have no idea how to manipulate ui elements from database class the correct way.

              You don't. The same way as you pass data to the database class (i.e. with a signal/slot connection), it will notify the dependent class of changes - again with a signal.

              R Offline
              R Offline
              realhamidrezakp
              wrote on 3 Dec 2018, 12:33 last edited by realhamidrezakp 12 Mar 2018, 12:34
              #6

              @kshegunov

              Which parts?

              i have read it again and understand , thanks :D.

              What errors do you get?

              i was trying to send a pointer to my database class from mainWindow to another form and get Segment fault error.

              That's good thinking, but what is the exact problem?

              how i must implement a database class to manage all database works like update listView in forms or insert data to database.

              You don't. The same way as you pass data to the database class (i.e. with a signal/slot connection), it will notify the dependent class of changes - again with a signal.

              so you mean i must update model and call (for example listView) for updating model ?
              for example in case of updating a search result list , what is the best option? update search results in database class or send items to form class and update them in there ?
              thank you for answering.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 3 Dec 2018, 22:39 last edited by
                #7

                Hi,

                You don't need to move your QSqlDatabase objects around. The technique is shown in the details of the class.

                You don't need to call anything on the view. If the model is properly implemented, the view will get notified and update itself.

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

                R 1 Reply Last reply 16 Jan 2019, 07:39
                3
                • SGaistS SGaist
                  3 Dec 2018, 22:39

                  Hi,

                  You don't need to move your QSqlDatabase objects around. The technique is shown in the details of the class.

                  You don't need to call anything on the view. If the model is properly implemented, the view will get notified and update itself.

                  R Offline
                  R Offline
                  realhamidrezakp
                  wrote on 16 Jan 2019, 07:39 last edited by
                  #8

                  @SGaist

                  You don't need to call anything on the view. If the model is properly implemented, the view will get notified and update itself.

                  so where i must modify model ?
                  i must send model with signal/slots to database class and modify it then pass to view ?
                  would be so nice if you clarify this for me. thanks

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 16 Jan 2019, 08:00 last edited by
                    #9

                    What do you mean by modify the model ?

                    You can see how to handle QtSQL models and views in the QtSQL modules examples.

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

                    R 1 Reply Last reply 16 Jan 2019, 08:32
                    3
                    • SGaistS SGaist
                      16 Jan 2019, 08:00

                      What do you mean by modify the model ?

                      You can see how to handle QtSQL models and views in the QtSQL modules examples.

                      R Offline
                      R Offline
                      realhamidrezakp
                      wrote on 16 Jan 2019, 08:32 last edited by
                      #10

                      What do you mean by modify the model ?

                      i mean updating model , like a search window that needs to update model every time user change input.

                      You can see how to handle QtSQL models and views in the QtSQL modules examples.

                      i saw it, and figure out some of examples. but i need to read more about MVC in qt.
                      thanks you.

                      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