Unsolved QMAKE
-
You are storing a local QSqlDatabase object and are recreating a new connection that is replacing the one stored in the variable.
-
@SGaist So how can it be solved?
-
First thing, as the QSqlDatabase documentation explains: do not do that and if you remove the database connection, follow the documentation of the removeDatabase method..
-
Thanks,I gone through that documention,but still not clear,I uninstalled the mysql server which was connected to the app at first,but after trying to connect for second ,am facing that error.
The first connection used the default connection name. Am confused on how to desconnect the database with a default name.I have seen this function QSqldatabase::connectionName() but on how to use it am not sure.Please any help will be appreciated. -
How are you using QSqlDatabase in your code ?
-
@SGaist
Here is the code:QT += core gui sql printsupport greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++11
.h file
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include<QtSql> #include<QSqlDatabase> #include<QMessageBox> #include<mywork.h> //#include<payfee.h> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); void closedb(); private slots: void on_LOGIN_clicked(); void on_REGISTER_clicked(); void on_LOGIN_2_clicked(); private: Ui::MainWindow *ui; QSqlDatabase database; }; #endif // MAINWINDOW_H
d.cpp file
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->USERNAME_REG->setPlaceholderText("ENTER USERNAME"); ui->E_MAIL->setPlaceholderText("ENTER YOUR E-MAIL ADDRESS"); ui->PASSWORD_REG->setPlaceholderText("ENTER YOUR PASSWORD"); ui->PHONE->setPlaceholderText("ENTER PHONE NUMBER"); ui->USERNAME_LOGIN->setPlaceholderText("ENTER USERNAME"); ui->PASSWORD_LOGIN->setPlaceholderText("ENTER PASSWORD"); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_LOGIN_clicked() { QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("127.0.0.1"); db.setUserName("root"); db.setPassword(""); db.setDatabaseName("mysql"); if(db.open()) //database connections { QMessageBox::information(this,"connection","SYSTEM READY\nYOU CAN NOW REGISTER OR LOGIN!"); } else { QMessageBox::information(this,"not connected","SYSTEM NOT CONNECTED"); } } void MainWindow::on_REGISTER_clicked() { QString username = ui->USERNAME_REG->text(); QString email=ui->E_MAIL->text(); QString password =ui->PASSWORD_REG->text(); QString phone =ui->PHONE->text(); if(username=="" || email==""|| password==""||phone=="") { QMessageBox::warning(this,"warning","PLEASE ENTER ALL THE REQUIRED DETAILS"); } else {//checking to avoid duplication of data QSqlQuery query; query.prepare("SELECT USERNAME,EMAIL,PHONE FROM users WHERE USERNAME=:USERNAME AND EMAIL=:EMAIL AND PHONE=:PHONE"); query.bindValue(":USERNAME",username); query.bindValue(":EMAIL",email); query.bindValue(":PHONE",phone); if(query.exec()){ //QMessageBox::warning(this,"warninng","query successful!"); } if(query.size()>0){ QMessageBox::warning(this,"WARNING","CHECK YOUR NAME,E-MAIL OR PHONE NUMBER PLEASE\n" "ONE OF THESE HAVE BEEN USED BY OTHER USERS ALREADY!"); } // QSqlQuery qry; else { //running insert query QSqlQuery qry; qry.prepare("INSERT INTO users(USERNAME,EMAIL,PASSWORD,PHONE)" "VALUES(:USERNAME, :EMAIL, :PASSWORD, :PHONE)"); qry.bindValue(":USERNAME",username); qry.bindValue(":EMAIL",email); qry.bindValue(":PASSWORD",password); qry.bindValue(":PHONE",phone); if(qry.exec()) { QMessageBox::information(this,"WELCOME","YOU HAVE SUCCESSFULLY REGISTERED!\nYOU NOW LOGIN!"); } else { QMessageBox::warning(this,"WARNING!","PLEASE TURN ON THE SYSTEM!"); } } } } void MainWindow::on_LOGIN_2_clicked() { QString username=ui->USERNAME_LOGIN->text(); QString password=ui->PASSWORD_LOGIN->text(); QSqlQuery query(QSqlDatabase::database("Myconnect")); query.prepare("SELECT USERNAME,PASSWORD FROM users WHERE USERNAME=:USERNAME AND PASSWORD=:PASSWORD"); query.bindValue(":USERNAME",username); query.bindValue(":PASSWORD",password); if(!query.exec()){ //QMessageBox::warning(this,"warninng","PLEASE TURN ON THE SYSTEM!"); } if(query.size()>0){ //window two hide(); MYWORK work; work.setModal(true); work.exec(); } else { QMessageBox::warning(this,"CHENJEZO!","NOT SUCCESSFUL!\n" "TURN ON THE SYSTEM OR ENTER CORRECT DETAILS!"); } }
-
QSqlDatabase database;
As suggested already remove that (you do not even use it in the code you posted).
There is no need to keep QSqlDatabase instance (explained in the documentation).QSqlQuery query(QSqlDatabase::database("Myconnect")); - where do you actually add this database? Please read https://doc.qt.io/qt-5/qsqldatabase.html#database
"If no connectionName is specified the default connection is used. If connectionName does not exist in the list of databases, an invalid connection is returned."
" -
@jsulm
Thanks for your help .The Qsqldatabase database has been used in a difference class when calling for the instance database.But the main problem is that I can't connect to the database any more .am always told qmsql drivers not loaded.old connection removed.My am is to remove that old
connectionn so that I can connect to the database again. -
But why do you need to remove the connection in the first place ? Can't you just close it, update the information and open it again ?
How exactly are you using QSqlDatabase ?
-
@SGaist
I don't really want to remove the connection,its like it became unconnected when i unistalled qt5 .9.9 and installal qt 5.12.5, i tried to install qt 5.9.9 but it did not become connected again.on how QSqlDatabase is being used,It was used to create an object database which is being called when i want to access the database .below is an example of how it has been used.
private slots: void on_LOGIN_clicked(); void on_REGISTER_clicked(); void on_LOGIN_2_clicked(); private: Ui::MainWindow *ui; QSqlDatabase database;
MUSIC, CLASS.
#include "music.h" #include "ui_music.h" MUSIC::MUSIC(QWidget *parent) : QDialog(parent), ui(new Ui::MUSIC) { ui->setupUi(this); ui->SURNAME->setPlaceholderText("ENTER SURNAME"); ui->GRADE->setPlaceholderText("INTER GRADE, e.g 9"); ui->CLASS->setPlaceholderText("ENTER CLASS e.g 9A"); ui->AMOUNT->setPlaceholderText("ENTER AMOUNT e.g 100"); ui->OTHERNAME->setPlaceholderText("ENTER OTHER NAME(S)"); } MUSIC::~MUSIC() { delete ui; } void MUSIC::on_ENTER_FEE_clicked() { QString surname =ui->SURNAME->text(); QString lastname=ui->OTHERNAME->text(); QString clas= ui->CLASS->text(); QString amount=ui->AMOUNT->text(); QString grade =ui->GRADE->text(); QString date=ui->DATE->text(); if(surname=="" || grade==""|| clas==""||amount==""||lastname==""||date=="") { QMessageBox::warning(this,"warning","PLEASE ENTER ALL THE REQUIRED DETAILS"); } else { QSqlDatabase::database(); QSqlQuery query; query.prepare("SELECT SURNAME,LASTNAME FROM music WHERE SURNAME=:surname AND LASTNAME=:lastname"); query.bindValue(":surname",surname); query.bindValue(":lastname",lastname); if(query.exec()){ //QMessageBox::warning(this,"warninng","query successful!"); } if(query.size()>0){ QMessageBox::warning(this,"WARNING","THE NAME HAS BEEN ENTERED ALREADY!\nUSE INITIAL IF ITS A DIFFERENT NAME!"); } // QSqlQuery qry; else { query.prepare("INSERT INTO music (SURNAME,LASTNAME,GRADE,CLASS,AMOUNT,DATE)" "VALUES(:SURNAME, :LASTNAME, :GRADE, :CLASS, :AMOUNT, :DATE)"); query.bindValue(":SURNAME",surname); query.bindValue(":GRADE",grade); query.bindValue(":CLASS",clas); query.bindValue(":AMOUNT",amount); query.bindValue(":LASTNAME",lastname); query.bindValue(":DATE",date); if(query.exec()) { QMessageBox::information(this,"inserted","PAYMENTS RECORDED SUCCESSFULLY!"); } else { QMessageBox::warning(this,"warning","PAYMENT NOT RECORDED!"); } } } }
-
Do not keep a QSqlDatabase member variable.
It's explained in the class documentation. -
Hi,
- your first error was due to : QMAKE_USE += mysql in mysql.pro
Comment it and everything will move to the next step - your second error: was due to the qmysql driver (missing driver or not compatible with your version)
- it is Ok to use QT with wampp mysql ( i'm using it with xampp )
- remake the driver, and that will solve the issue of : QMYSQL driver not loaded
- follow this reply you will achieve the connection :: Tested Solution
good luck.
- your first error was due to : QMAKE_USE += mysql in mysql.pro
-
@tmapp
Okay thanks for your help,am just not clear on how to remake drivers . -
am just not clear on how to remake drivers .
RTM: https://doc.qt.io/qt-5/sql-driver.html#building-the-drivers or search the forum.
-
@Christian-Ehrlicher
Okay thanks -