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



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


  • Qt Champions 2018



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



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


  • Qt Champions 2017

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



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


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

    What do you mean by modify the model ?

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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.