using model to fill tableView with QMYSQL
-
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. -
@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
} -
@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. -
@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.@JonB I have posted the code right at the top
Exactly that code brings this errorvoid tableFill::fill_table(){ QString command = "select * from countryDetails;"; { QString databaseName = "root"; QString databasePassword= ""; QString databaseUsername= "root"; QString databaseHost = "localhost"; int databaseName = 3306; QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setDatabaseName(databaseName); db.setPort(databasePort); db.setHostName(databaseHostname); db.setUserName(databaseUsername); db.setPassword(databasePassword); if(db.open()){ QSqlQueryModel *model = new QSqlQueryModel(); QSqlQuery *query = new QSqlQuery(db); query->prepare(command); if(query->exec()){ model->setQuery(*query); ui->information->setModel(model); //setting the model }else{ QMessageBox::warning(this,"ERrors in query exec" ,"Error while using query execution"+query->lastError().text()); } }else{ QMessageBox::warning(this,"Error in database ", "Database is not connected "+db.lastError().text()); } } QSqlDatabase::removeDatabase("qt_sql_default_connection"); }
-
@JonB I have posted the code right at the top
Exactly that code brings this errorvoid tableFill::fill_table(){ QString command = "select * from countryDetails;"; { QString databaseName = "root"; QString databasePassword= ""; QString databaseUsername= "root"; QString databaseHost = "localhost"; int databaseName = 3306; QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setDatabaseName(databaseName); db.setPort(databasePort); db.setHostName(databaseHostname); db.setUserName(databaseUsername); db.setPassword(databasePassword); if(db.open()){ QSqlQueryModel *model = new QSqlQueryModel(); QSqlQuery *query = new QSqlQuery(db); query->prepare(command); if(query->exec()){ model->setQuery(*query); ui->information->setModel(model); //setting the model }else{ QMessageBox::warning(this,"ERrors in query exec" ,"Error while using query execution"+query->lastError().text()); } }else{ QMessageBox::warning(this,"Error in database ", "Database is not connected "+db.lastError().text()); } } QSqlDatabase::removeDatabase("qt_sql_default_connection"); }
@Thank-You And we told you at least two times that you must not remove the database connection while there is an open query for your QSqlTableModel!
-
@JonB I have posted the code right at the top
Exactly that code brings this errorvoid tableFill::fill_table(){ QString command = "select * from countryDetails;"; { QString databaseName = "root"; QString databasePassword= ""; QString databaseUsername= "root"; QString databaseHost = "localhost"; int databaseName = 3306; QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setDatabaseName(databaseName); db.setPort(databasePort); db.setHostName(databaseHostname); db.setUserName(databaseUsername); db.setPassword(databasePassword); if(db.open()){ QSqlQueryModel *model = new QSqlQueryModel(); QSqlQuery *query = new QSqlQuery(db); query->prepare(command); if(query->exec()){ model->setQuery(*query); ui->information->setModel(model); //setting the model }else{ QMessageBox::warning(this,"ERrors in query exec" ,"Error while using query execution"+query->lastError().text()); } }else{ QMessageBox::warning(this,"Error in database ", "Database is not connected "+db.lastError().text()); } } QSqlDatabase::removeDatabase("qt_sql_default_connection"); }
@Thank-You
This is exactly the code as it was. It hasremoveDatabase()
immediately after the message box show. Which we have told you multiple times is wrong. It does not have theQMessageBox::information(this,"NOw the database is shown", " This is also shown");
you later claimed to have put in, nor the output you showed in your last screenshot. Which doesn't correspond to that either.
What else is there to say? You don't listen, you don't adjust your code, you show screenshots which don't pertain to the code.
I think we are all done. We don't know what answer you are prepared to accept. I suggest you keep your code exactly as you have it, since that is the only thing you are prepared to accept. Bye.
-
@Thank-You And we told you at least two times that you must not remove the database connection while there is an open query for your QSqlTableModel!
@Christian-Ehrlicher And when to remove it
Or leave it without removing ?
I said that it will give me errors,
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
I don't want this application to have this error?OR
How can I get the signal when query does not require the database.@JonB Please refer to my last code. It is exactly the same, Nothing different. May be I gave you the example of another table fill.
Just see the last one and say exactly what is the problem just for the last time. -
@Christian-Ehrlicher And when to remove it
Or leave it without removing ?
I said that it will give me errors,
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
I don't want this application to have this error?OR
How can I get the signal when query does not require the database.@JonB Please refer to my last code. It is exactly the same, Nothing different. May be I gave you the example of another table fill.
Just see the last one and say exactly what is the problem just for the last time.@Thank-You said in using model to fill tableView with QMYSQL:
I said that it will give me errors,
And I said you to not to add the database again and again, do it just once. I really don't know why it is necessary to repeat same things again and again...
Remove the database when it is not needed anymore (for example in the destructor of your class). -
@Thank-You said in using model to fill tableView with QMYSQL:
I said that it will give me errors,
And I said you to not to add the database again and again, do it just once. I really don't know why it is necessary to repeat same things again and again...
Remove the database when it is not needed anymore (for example in the destructor of your class). -
Maybe an analogy would help with the advice that everybody has given. Pretend like running a query on a database is like teaching a dog a new trick. You want the dog to be alive when you teach the trick and still be alive to enjoy watching the trick.
What people are saying is that in your fill table function, you are creating a dog, teaching a trick, killing the dog, and wondering how come it doesn't perform the trick.
In conclusion, you need your dog to live as long as the lifetime that you want the user to see your table. Did this help?