When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash
-
Hi,
BDLogin is a member variable, isn't it ?
There's a warning in the QSqlDatabase documentation that explicitly say that you shall not keep QSqlDatabase object as member variables.Hi, SGainst!
I will give almost the same answer I gave to mrjj, with some additions:
I open the same database in two different instances. The name of the database is 'REGISTERS'. I need to open it in the login window, instance ‘BDLogin’ and when the username and password are validated, the main window opens. Then I close the instance 'BDLogin', close the login window and open the main window. In the builder area, I create a new instance (BDCadasters) and open the database again.
Although I close one instance to open another, in the debug area there are always two messages stating that there was an open database and that it is being unusable for another to open. Really do not understand.
Detail: The database has 3 tables. I use different tables in each of the windows.
Despite all my explanation, I was left with a little doubt: what is member variable? Where in the code should I NOT keep it?
I'm new to QT and C ++, so I will answer your question as I know: BDLogin is an instance of QSqldatabase, created to open the database. Would that be a member variable?
-
Hi, SGainst!
I will give almost the same answer I gave to mrjj, with some additions:
I open the same database in two different instances. The name of the database is 'REGISTERS'. I need to open it in the login window, instance ‘BDLogin’ and when the username and password are validated, the main window opens. Then I close the instance 'BDLogin', close the login window and open the main window. In the builder area, I create a new instance (BDCadasters) and open the database again.
Although I close one instance to open another, in the debug area there are always two messages stating that there was an open database and that it is being unusable for another to open. Really do not understand.
Detail: The database has 3 tables. I use different tables in each of the windows.
Despite all my explanation, I was left with a little doubt: what is member variable? Where in the code should I NOT keep it?
I'm new to QT and C ++, so I will answer your question as I know: BDLogin is an instance of QSqldatabase, created to open the database. Would that be a member variable?
@Alexandre-Camelo @mrjj @fcarney @Christian-Ehrlicher @SGaist
One question for all of you trying to help me: I have declared all QSqlquery in the .h file so that they are available all the time, in any scope of the cpp file. I did wrong? Can I have multiple QSqlquery open at the same time? I realize that the error occurs when I am using these queries at the same time. Example: I search by ID, then do a search by name, and then delete the record. AT THAT EXACT TIME, the error occurs.
-
BUDIEEEEESSSSSSSSS !!!!!!!
I got to decipher the riddle !!!!!
Thanks to partner @fcarney! Your tip for running the program in debug mode was very valuable!
I noticed that the problem was NOT in any database or query.
There is a tablewidget on the form, which lists all customers. Whenever there is an update on the form, it is reflected in the tablewidget (additions, deletions, selections ...).
Similarly, when I select a record in tablewidget, this action is reflected in the form.
But I was using the "itemSelectonChanged" slot in the table widget. THAT WAS THE MISTAKE !!!!! Whenever some code updated tablewidget, the "itemSelectonChanged" slot was activated, generating an infinite loop that broke the code.
I changed the tablewidget slot to "itemDoubleClicked" and it's all settled!
Thank you very much to all of you. Each of your opinions helped me learn a little more about QT and C ++.
-
You should still fix the database warning as it may lead to crashes.
-
You should still fix the database warning as it may lead to crashes.
Exactly.
I haven't figured out how to do it yet.
I created a "Connection" class that contains two functions: 1) Open database; 2) Close database. It is through the functions of this class that I am opening the database. This class is in an .h file, which I refer to when I need to open the DB.
Example: I compile the program and the login window opens. In your .h file, I create a new instance for the "Connection" class. In the cpp file builder is the "open database" command. When user data is validated, the login window is closed and I use the .close () command to close the database.
Shortly after this, the main window opens and the whole process above (new instance creation, database opening) is repeated. Even so, the database open error messages appear.
I thought the error would occur in the class when entering the DB connection type: BDCadastros = QSqlDatabase :: addDatabase ("QSQLITE"). However, when I remove this command from the class and place it as a global variable, the database is not opened. I do not know what to do.
Here's the code of the class I created (remembering that I put it in an .h file that I give <include> to other files):
<code>
class ConexaoCadastros
{
public:
QSqlDatabase BDCadastros; //Cria um objeto da classe QSqlDatabaseConexaoCadastros() //Coloca o tipo de banco de dados no CONSTRUTOR da classe { BDCadastros=QSqlDatabase::addDatabase("QSQLITE"); } //Função para abrir o BD bool Abrir() { BDCadastros.setDatabaseName (glbCAMINHOBDCADASTROS); //Seleciona o banco de dados if(!BDCadastros.open()) //Abre o BD, testando se ele foi aberto com sucesso { return false; } else { return true; } } //Função para fechar o BD void Fechar() { BDCadastros.close(); }
};
<code/>Do you have any suggestions?
-
@Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
I use the .close () command to close the database.
Why? Why don't you just follow the documentation, let the db open and get the connection with QSqlDatabase::database(QString)?
-
@Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
I use the .close () command to close the database.
Why? Why don't you just follow the documentation, let the db open and get the connection with QSqlDatabase::database(QString)?
@Christian-Ehrlicher said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
@Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
I use the .close () command to close the database.
Why? Why don't you just follow the documentation, let the db open and get the connection with QSqlDatabase::database(QString)?
As I said, I'm new to QT and C ++.
I tried in many ways to leave the database open and make all queries throughout the code.
But I can't.
Whenever I close one window and open another, the previous window's database closes (even if I don't use the close () command.) This way, I am forced to reopen the DB when I open the new window. It's worse: The open database message appears.
How do I leave the database open during all code execution?
-
@Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
How do I leave the database open during all code execution?
As stated in the documentation - open the database once with QSqlDatabase db = QSqlDatabase::addDatabase("..."); and get the db connection with QSqlDatabase db = QSqlDatabase::database(); ...
-
@Christian-Ehrlicher said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
@Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
I use the .close () command to close the database.
Why? Why don't you just follow the documentation, let the db open and get the connection with QSqlDatabase::database(QString)?
As I said, I'm new to QT and C ++.
I tried in many ways to leave the database open and make all queries throughout the code.
But I can't.
Whenever I close one window and open another, the previous window's database closes (even if I don't use the close () command.) This way, I am forced to reopen the DB when I open the new window. It's worse: The open database message appears.
How do I leave the database open during all code execution?
@Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
the previous window's database closes
Do you have a member variable in your window which holds the database connection?
@SGaist already pointed out that you should NOT do this.
QSqlDatabase manages all your connections for you and you can get any open connection using QSqlDatabase::database();
There is absolutely no need to have member variables holding the connection. -
@Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
the previous window's database closes
Do you have a member variable in your window which holds the database connection?
@SGaist already pointed out that you should NOT do this.
QSqlDatabase manages all your connections for you and you can get any open connection using QSqlDatabase::database();
There is absolutely no need to have member variables holding the connection.OK friends.
It worked.
Thank you very much.
One last question: You talked about "DOCUMENTATION". Where do I find her? On the QT website? No QT help?
All I have done is watching videos on youtube.
-
Literally I Googled "qt docs":
https://doc.qt.io/
Also, when searching for something specific search:
"qt json" or "qobject" or "qstring" etc -
Literally I Googled "qt docs":
https://doc.qt.io/
Also, when searching for something specific search:
"qt json" or "qobject" or "qstring" etc@fcarney said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
Literally I Googled "qt docs":
https://doc.qt.io/
Also, when searching for something specific search:
"qt json" or "qobject" or "qstring" etcThanks guys.
You guys helped me A LOT!
In a little while I'll be back to "bother" you again.
:-D
-
@Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
In a little while I'll be back to "bother" you again.
Its all about the "bother", no trouble...