There is a sqlite database. At the moment, connections to the database occur only when necessary, i.e. it took to get the data - connect, sample, disconnect. Accordingly, if there are several different types of data on the form, then for each of them these 3 operations are performed.

For example: with the form we get information about the cat. On the form of combobox'ы with the choice of breed, coloring, nursery. To fill each combobox , connect, fetch, disconnect is called.

Is it right to do so? Or is it better when you start the application to connect to the database and keep the connection until the application is closed?

  • in my experience, a method is created, in it a connection, a sample, a disconnect, a return of a sample. Again, it is not clear how and what your "several different types of data" respond to. - TimurVI
  • @TimurValiev added an example to the question. - Edward Izmalkov
  • if the "breeds, colors, nurseries" in different tables, it is logical to extract them separately - this adds flexibility even if you need to add value to the table, in this case my opinion, yes, that's right. - TimurVI

1 answer 1

Is it right to do so? Or is it better when you start the application to connect to the database and keep the connection until the application is closed?

If the database server and the client on the same host - come down and so, a connection for each sneeze. If in the middle there is a network exchange, it is better to have one connection per packet of requests.

In most cases, the intermediate approach is more reasonable - one connection for one object (of the same form), and not for the entire application. Create when opening a form, kill at closing.

  • I will clarify a little. From one form can change the data of several tables. Implemented by classes, each table has its own class. Information from the form is transmitted to a specific class, and he himself communicates with the database. In this case, do you recommend creating a connection when you start a form and pass it to classes, rather than launch your own class in each class? - Edward Izmalkov
  • No, everyone is responsible for himself and only for himself. If a class communicates with a database, then it is he who should provide OWN communication with the database, and not hope that the caller will provide it. As well as the form should not deal with the problems caused by her class. Ie, for example, an instance of an object in the constructor opens a connection, the procedures of the object uses it, and in the destructor the connection is closed. Plus handling of a disconnection event - for example, by restoring or opening a new one. - Akina
  • Then another question :) At the moment I have implemented this: the designer opens the connection, receives the data, closes it. Each property of the class when changing also opens a connection, changes the table, closes it. It is better to open in the constructor, and close in the destructor? With respect to sqlite, this will not be a source of problems, since sqlite doesn't like multiuser access? - Edward Izmalkov
  • I have no idea how best. What sqlite loves is generally not up to date. But just logic dictates that "every man for himself." And everyone will open this one for any sneeze, or they will always use one - this is up to you. As you wish. Wrong to say - all the rakes at your expense. - Akina
  • so the sqlite is specified in the question, respectively, and I want to know how best to work with it. - Edward Izmalkov