Solved QSqlQuery::prepare: database not open
-
Hello Friends
My Qt version is : 5.12.3
MYSQL version is : MySQL Server 5.6I am get a differnt type error when i am using MYSQL with qt
when i try to load data in QComboBox that time following error are occurred.
QSqlQuery::prepare: database not open
QSqlError("", "Driver not loaded", "Driver not loaded")
("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7")i did't get any solution please help me to solve this problem
-
-
Is it only this code? if so show the code please.
If its trying to connect in general debug your module trace and see what module it is failing on will give you an idea what module you may need to add/move. Make sure your libmysql.dll is in the right place compared to what compiler you are using.
--James
-
@Ketan__Patel__0011 This issue was asked many time here - please use search function.
-
i am using MSVC2015 kit
Yes, libmysql.dll is it in proper and right place
even i can perform any mysql query like insert, update or delete
My Code Is :
void MainWindow::Populate_User_Combo(QComboBox *combo) { Connect_DB(); QSqlQueryModel *model = new QSqlQueryModel(); QSqlQuery Query_load_camera_names(db); Query_load_camera_names.prepare("SELECT Names FROM User_Master"); Query_load_camera_names.exec(); model->setQuery(Query_load_camera_names); combo->setModel(model); Disconnect_DB(); } void MainWindow::Connect_DB() { if(db.isOpen() == true) { db.close(); } else { qDebug() << db.lastError(); qDebug() << QSqlDatabase::drivers(); db.open(); qDebug() << "Database Is Open : " << db.isOpen(); } } void MainWindow::Disconnect_DB() { db.close(); qDebug() << "Database Is Disconnected"; }
-
So if I understand you correctly you can insert/update/delete no problem but this code you posted is the problem:
perhaps im missing something but it appears you call
Connect_DB();
then the first line of it you:if(db.isOpen() == true) { db.close(); }
close the db before trying to access it for your combo box
--James
-
Hey now a new problem when i call void MainWindow::Populate_User_Combo(QComboBox combo) first time it will work completely And Data are stored in QComboBox But when i call this function second time that 's it's not working
and this errors are raised:
QSqlQuery::prepare: database not open
QSqlError("", "Driver not loaded", "Driver not loaded")
("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7")help me to solve this problem
-
Hi,
You did not configure any connection.
Side point: do not keep a class member QSqlDatabase variable as explained in the documentation. -
@SGaist Not to hijack this thread buy why not keep a class member QSqlDatabase variable? I do not see that in the https://doc.qt.io/qt-5/sql-connecting.html docs unless I missed it. I do it in my program without issues. I only use one db so there is no need for one I know however it was just code from when I was learning and I am just curious as to why this is wrong?
@Ketan__Patel__0011
You would have to show your code again now that you have it working but I guessing you close the db and do not reopen it?--James
-
@JSher In the QSqlDatabase details. See the first warning.
-
@SGaist Ahh gotcha thanks":)
--James
-