D
D
Dmitry Burnashev2018-11-27 08:31:20
Qt
Dmitry Burnashev, 2018-11-27 08:31:20

How to properly design a class for working with a database?

I have been working (for a month now) in a small company, writing a project in C ++ using Qt, SQLite was chosen as the database. There are no higher level programmers nearby.
I decided to design a separate module for working with the database. There are tables in the database: product, seller, sale. In one of the windows of the program, a table into which data from the database will be loaded. You can also create a new entry there. When you click the Save button, changes are saved / a new entry appears if it does not exist.
I already have a DataBase class responsible for connecting to the database, opening/closing the connection, and sending the query string to the database, and so on.
(0) You can add a method to the same class, in whose parameters you need to specify product attributes, for example, for further addition to the database, but there are quite a lot of them, and this does not seem to me a good approach.
You can create separate classes for each of the database tables.
In this case, there are several options:
(1) In each class, create methods that send a query string to the DataBase class, but they will require an object instance. If the database grows, then I think when it is displayed, creating an object for each database entry will significantly slow down the program.
(2) Make these methods static, sending a signal (event), for example, to add a record, which will be received by a slot (method, event handler) of the DataBase class. But then there is a problem with connecting the signal and the slot, because the connect method needs an object instance.
(3) You can make structures instead of classes, but in this case we get a slightly improved version (0).
Which of these should you choose? Or is there some better option?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artemy, 2018-11-27
@dilepremon

Surely there is some kind of ready-made ORM for C ++, so as not to reinvent the wheel, but if not, then you need to choose between the Active Record and DataMapper patterns

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question