N
N
NeoVic20062021-08-03 01:27:46
Python
NeoVic2006, 2021-08-03 01:27:46

Why are classes needed when working with MySQL?

Hello.
I'm not an experienced developer, so this will be a very banal question.

I have a small project "Hotel". I was told to use there 2 classes: Hotel, and Room.
When a Room object is created, it is added to the List argument in the Hotel Object.

Accordingly, I store all the objects of the Rooms in a sheet in the Hotel. And when I need to indicate that the Client has registered in the number, I call the Number object I need, and change a couple of parameters there, like the number is "Busy" and add the client's name to this object.

The next step I was told was to add a MySQL database in order to store the data there. Then the question arises why the Classes Hotel and Number are needed, if in any case they will not be stored in the database. The database will store information in the form: Number, Client, whether you can smoke or not, etc. No classes needed then. Why create objects if at the end of the script they are all simply deleted.

Accordingly, the question in architecture is: why create class objects at all, if all useful information is stored in the database?

And creating Objects + adding more information to the database, then when deleting something from the database, you need to delete it from the object ..... It makes no sense.

Please explain to a noob how this is all organized.

Thank you very much.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ivan Shumov, 2021-08-03
@inoise

At a general level, classes and objects allow you to simply conveniently work with the subject area, in the same way, according to the same rules, while hiding the implementation. You can read about OOP in general and about SOLID separately. If the database is connected, then either something is not correctly understood or something is not given, but it is felt that the maximum is a training project. Of course, in a normal situation, all this information will be stored in the database in a normalized form.
Well, deletion in the database should not lead to a change in the object, on the contrary - the object in this case is an interface for interacting with the database and it is not necessary to allow the possibility of performing operations on the database outside of them

S
SagePtr, 2021-08-03
@SagePtr

In order not to rewrite the entire project later, if you have to replace MySQL with another DBMS. And it will be enough just to edit those classes that work directly with the database, the rest should not work directly with the database, only through these classes.

S
Sergey Gornostaev, 2021-08-03
@sergey-gornostaev

Classes are a way of organizing code. It has nothing to do with storing data in a database.

T
ThunderCat, 2021-08-03
@ThunderCat

In short, I understand that your question briefly sounds like this: Why do we need classes if they cannot store information, but you still need a database?
In short - data is NOT STORED in objects, objects are designed to organize code and have nothing to do with storage. Objects are empty at the time of creation, or are initialized with some data from the outside (for example, from the database), for better understanding, and if we simplify a lot, these are complicated arrays that can also manipulate their data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question