Unsolved using model to fill tableView with QMYSQL
-
Do not remove the database. That’s the problem
-
@Thank-You said in using model to fill tableView with QMYSQL:
QSqlQuery *query = new QSqlQuery(db);
And don't create it on the heap - otherwise the query is leaking.
-
@Thank-You said in using model to fill tableView with QMYSQL:
db.open(); if(db.open()){
And don't do that, it makes no sense, if you understand what you are doing.
-
@JonB Ok , I can remove it
But may be I was checking it only 🤣🤣🤣😂 -
QSqlQueryModel *model = new QSqlQueryModel(); QSqlQuery query(db); query.prepare(command); if(query.exec()){ model->setQuery(query); ui->report->setModel(model); }
This one doesn't work either
How can I solve it?
-
@Thank-You said in using model to fill tableView with QMYSQL:
How can I solve it?
Did you read @VRonin 's message?
-
But why the same thing is working with
SQLITE
and not with
MYSQL -
@Thank-You
Who knows, they are different databases with different driver code & behaviour.Why do you remove the database? What behaviour do you expect after the remove? Why do you care if the behaviour is different between SQLite versus MySQL if the code is wrong and can be corrected?
-
@Christian-Ehrlicher
Yes it worksQSqlDatabasePrivate::addDatabase: duplicate connection name 'report', old connection removed.
But this is error is very very disgusting. Like we should remove the connection,
Can you say another answer or different way??@JonB Because I read somewhere that removing database is always great
https://doc.qt.io/qt-5/qsqldatabase.html#removeDatabase -
@Thank-You
But you have an ongoingui->information->setModel(model)
which is still using the database, and you wonder why it goes wrong after you have done aremoveDatabase()
? Where does it say that situation is a good idea? -
@Thank-You said in using model to fill tableView with QMYSQL:
QSqlDatabasePrivate::addDatabase: duplicate connection name 'report', old connection removed.
Simply add the database once...
-
@jsulm
https://doc.qt.io/qt-5/qsqldatabase.html#removeDatabase
Warning: There should be no open queries on the database connection when this function is called, otherwise a resource leak will occur.{ QSqlDatabase db = QSqlDatabase::database("sales"); QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db); } // Both "db" and "query" are destroyed because they are out of scope QSqlDatabase::removeDatabase("sales"); // correct
From the official documentation I found that queries should not be open.
I don't know wether I am understanding what you are saying or not. If you mean different thing, please describe in simpler words -
@Thank-You I was talking about ADDING database.
"QSqlDatabasePrivate::addDatabase: duplicate connection name 'report', old connection removed." - this means you're trying to add a connection which already was added...What you posted now shows again that you still are removing the database though you was told several times already to not to do so. Why?!
-
@Thank-You said in using model to fill tableView with QMYSQL:
From the official documentation I found that queries should not be open.
Again: in your case the query is not out of scope/destroyed because you have:
QSqlQueryModel *model = new QSqlQueryModel(); QSqlQuery *query = new QSqlQuery(db); model->setQuery(*query); ui->information->setModel(model)
-
@jsulm
If I am right ,
We get this error when we didn't close the database properly.Please see my code at very top ,
There I created database and after executing it
I just removed the databaseYes as said by @Christian-Ehrlicher and @VRonin If I remove this line it actually works but this error is obtained.
Again I will tell you ,
It was working fine when I used the "SQLITE" database, The same code. Even there is no fault in query too.
What is difference between using these database, Is it due to ASYNCHRONUS thing?
Am I right ?? -
@Thank-You
We have tried to tell you. And one more time: just because bad code might work with SQLite but behave differently with MySQL does not mean we can tell you why you might get away with it in some circumstances.Why don't you just think about what you are doing? If you add database, open a query onto it and then set a permanent model to that which is referenced by an outside-world
QTableView
, and then you remove the database while that is still the case, what do you actually expect to happen? Can you not see this might be a problem?- Don't remove the database while you have a view/model using it.
- Don't re-add the same database while you have previously added it and not yet removed it.
Frankly I don't understand your whole architecture of opening a database while you are displaying a message box, and leaving it to persist into the outside world. The usual approach is to open a database initially/first time wanted and persist it till you are ready to close it and never want it again. I wouldn't be adding/removing the same database repeatedly.
-
Don't remove the database while you have a view/model using it
How can I know that it is using the database or not??
I just tried inserting QMessageBox when
if(query->exec()){ // same as the upper code here QMessageBox::information(this,"NOw the database is shown", " This is also shown"); }
After clicking OK,
The data vanishes again
@jsulm @JonB can you see this? -
@Thank-You
After showing the message box, does your code executeQSqlDatabase::removeDatabase()
? Just a "yes" or "no" answer. -
@JonB
No it doesn't
It executes
removeDatabase
at the very end of the program
like{
db initialize
execute query and set model
show the message box
}
remove the databaseor you can see the code same but there is
messagebox inside if(query->exec){
// all execution like model etc
messagebox
} -
@Thank-You
If you really want anyone to look at this, show your actual relevant, minimal code which reproduces the issue, not a description of it.