Unsolved Using database connection
-
Hi,
I have two classes, Additem and dbManager. I created a database connection in dbManager:void dbManager::mydbManager(const QString &fileQString) { db = QSqlDatabase::addDatabase ("QSQLITE","Friend"); db.setDatabaseName (fileQString); if(!db.open ()) { qDebug() << "Database connection Friend is not open!"; } else { qDebug() << "Database connection Friend is open!"; } }
When I run a the program it works, qDebug says the connection is open.
I tried to use it in Additem like this:#include "additem.h" #include "ui_additem.h" Additem::Additem(QWidget *parent) : QDialog(parent), ui(new Ui::Additem) { ui->setupUi(this); connection(); void Additem::connection() { dbManager *mdbManager = new dbManager; mdbManager->mydbManager (fileQstring); }
None of the functions in Additem can use the connection, all of them says that the database connection is not open. How can I use this connection properly? Thank you.
-
you do all is correct. In you case, when you get message "Database connection Friend is open!";
it is mean that connection is established correct and you can continue work with DB, execute other command. -
Hi,
You should print the error rather that just the message saying it didn't opened the connection.
-
Please change like below
void Additem::connection() { dbManager *mdbManager = new dbManager (fileQstring); }
-
@SGaist
I modified the qDebug() line like this:qDebug() << "Database connection Friend is not open!" << db.lastError ();
The actual error message is "Driver not loaded". It works in dbManager ( I assume it is also loaded), but clearly not in Additem. Is it possible to use a db connection from an other class like I'm trying or I'm supposed to deal with the db in the same class?
-
@SGaist
When I copydb = QSqlDatabase::addDatabase ("QSQLITE","Friend"); db.setDatabaseName (fileQstring);
directly into Additem everything works.
-
This post is deleted! -
small correction, please try now
void Additem::connection() { dbManager *mdbManager = new dbManager (fileQstring); } void dbManager::mydbManager(const QString &fileQString) { QSqlDatabase db = QSqlDatabase::addDatabase ("QSQLITE","Friend"); db.setDatabaseName (fileQString); if(!db.open ()) { qDebug() << "Database connection Friend is not open!" << db.lastError (); } else { qDebug() << "Database connection Friend is open!"; } }
-
Can you just share the project somewhere so we can take a look at the complete code ?
Have this spread across several files makes it difficult to find what is going wrong.
-
@SGaist
Thank you. Here are the files:
additem.h
dbmanager.h
mainwindow.h
review.h
additem.cpp
dbmanager.cpp
main.cpp
mainwindow.cpp
review.cpp -
Do you really need three different connections ?
It really looks like you are over-engineering it.
Like I already said: you can use different connections but there is no benefit in adding/removing them all the time if there's no need. And there's clearly no need in your case. Just setup the connection(s) once and then retrieve the one you want to use with
QSqlDatabase::database
. -
@SGaist
If I create a connection in dbmanager.cpp like this:db = QSqlDatabase::addDatabase ("QSQLITE","Friend"); db.setDatabaseName (fileQString);
how can I retrieve this connection in the other classes? When I create an object like
dbManager *mdbManager = new dbManager(); mdbManager->mydbManager (fileQstring);
in additem.cpp I keep getting the message "driver not loaded". What code to use to retrieve the same connection in all the 3 classes (you are right I don't need more than one connection).
Thank you. -
The "Driver not loaded" error is pretty surprising unless it happens on a deployed application which has the SQL plugins missing.
As for how to retrieve the connection, I've already wrote it: QSqlDatabase::database
-
It is not a deployed application.
-
Then you should add the QT_DEBUG_PLUGINS environment variable to the Run part of the Project panel and set it to 1 to see what is happening with the plugin load.
-
@SGaist
After removing line 606 from additem.cppQSqlDatabase::removeDatabase ("Friend");
I don't have the the driver not loaded error message.
-
Well indeed, if you remove the database connection and try to access it again without prior setup, the error message makes more sense.