How to use an Object of type QSqlTableModel in another function of the same class?
-
I got it now. I germany one would say "I was standing on the hose" I think it's best translated as: "I was drawing a blank" :D
I don't know what exactly was that hard for me to understand that but now I got it. So if i set the Database once in any source file by executing this:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName(DBHOSTNAME); db.setDatabaseName(DBNAME); db.setUserName(DBUSERNAME); db.setPassword(DBPASSWORD);
then I can get that connection always and everywhere inside my project with simply calling
QSqlDatabase db = QSqlDatabase::database();
how can I make it work with several database connections? Let's say I have two different databases...
Would i then add them like so: ?
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","MyDB1"); db.setHostName(DBHOSTNAME); db.setDatabaseName(DBNAME); db.setUserName(DBUSERNAME); db.setPassword(DBPASSWORD); QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","MyDB2"); db.setHostName(DBHOSTNAME); db.setDatabaseName(DBNAME); db.setUserName(DBUSERNAME); db.setPassword(DBPASSWORD);
-
I got it now. I germany one would say "I was standing on the hose" I think it's best translated as: "I was drawing a blank" :D
I don't know what exactly was that hard for me to understand that but now I got it. So if i set the Database once in any source file by executing this:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName(DBHOSTNAME); db.setDatabaseName(DBNAME); db.setUserName(DBUSERNAME); db.setPassword(DBPASSWORD);
then I can get that connection always and everywhere inside my project with simply calling
QSqlDatabase db = QSqlDatabase::database();
how can I make it work with several database connections? Let's say I have two different databases...
Would i then add them like so: ?
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","MyDB1"); db.setHostName(DBHOSTNAME); db.setDatabaseName(DBNAME); db.setUserName(DBUSERNAME); db.setPassword(DBPASSWORD); QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","MyDB2"); db.setHostName(DBHOSTNAME); db.setDatabaseName(DBNAME); db.setUserName(DBUSERNAME); db.setPassword(DBPASSWORD);
-
I tried to reply here but akismet is constantly flagging my reply as spam. Therefore I posted my reply to Pastebin:
https://pastebin.com/e2SVXZVbHere I made an error. It was meant so say:
"In any source-file where a database connection is needed I can now use the class DatabaseManager to call QSqlDatabase DB_Connection = DatabaseManager::instance();
So I can later check for isOpen(). That seems a lot better to me." -
@mooswitz in addition to what my fellows wrote: the SQL related classes you use offer error methods that allow you to learn what went wrong. Using them will help you more than just printing "something wrong happened".
-
Thank you both for your advices but I indeed used the .lastError() and related functions already. I did'nt mention them in my first posts because initially QSqlDatabase was'nt my problem. Although I made mistakes that I now have corrected. It's working perfect now.
My concern was: how can I use QSqlTableModel to trigger .submitAll(); via a button. But since I use a member variable it works.
I wonder if there is a similar approach to it like with QSqlDatabase::database(); now that I know how to use that it's super nice.But I think that my initial problem got solved by correcting the database connection. So thanks. I consider this thread as solved.
Greetz, mooswitz
-
Thank you both for your advices but I indeed used the .lastError() and related functions already. I did'nt mention them in my first posts because initially QSqlDatabase was'nt my problem. Although I made mistakes that I now have corrected. It's working perfect now.
My concern was: how can I use QSqlTableModel to trigger .submitAll(); via a button. But since I use a member variable it works.
I wonder if there is a similar approach to it like with QSqlDatabase::database(); now that I know how to use that it's super nice.But I think that my initial problem got solved by correcting the database connection. So thanks. I consider this thread as solved.
Greetz, mooswitz
@mooswitz said in How to use an Object of type QSqlTableModel in another function of the same class?:
My concern was: how can I use QSqlTableModel to trigger .submitAll(); via a button. But since I use a member variable it works.
I wonder if there is a similar approach to it like with QSqlDatabase::database(); now that I know how to use that it's super nice.Don't, that would be unclean. It is not a design pattern that would fit that use case.
-
@mooswitz said in How to use an Object of type QSqlTableModel in another function of the same class?:
My concern was: how can I use QSqlTableModel to trigger .submitAll(); via a button. But since I use a member variable it works.
I wonder if there is a similar approach to it like with QSqlDatabase::database(); now that I know how to use that it's super nice.Don't, that would be unclean. It is not a design pattern that would fit that use case.
@SGaist said in How to use an Object of type QSqlTableModel in another function of the same class?:
Don't, that would be unclean. It is not a design pattern that would fit that use case.
Okay. So What would be the better approach? I have taken the time now to figure out what would work better instead... so first: Why is there the "submitAll()" method, if I should'nt use it with a Button or sth. similar to trigger it? Or did you mean that I should not use the member variable for that?
What I want to do is the following: You (as a user) get presented with a TableView, showing some Data from a mariadb. This Data (in my case userdata) can be edited here. I do not want it to update directly after change. So that's Why I want a button to trigger that. Regarding the option to use QSqlTableModel for that - what is then the better option to trigger an Insert to the db with the Data loaded with the TableModel?
-
The unclean part was about trying to have a global QSqlTableModel in a similar fashion to QSqlDatabase.
For the rest, you did thing correctly.You could even simplify a bit by connecting the button directly to the submitAll function.Bad suggestion, you'd lose error handling. -
Okay. Thanks a lot!
How would I do that? I mean: connecting the button directly to the function?