Problems with MySql Database calls in C++ Classes

Multi tool use


Problems with MySql Database calls in C++ Classes
I'm currently developing a desktop app that acesses a remote MySql database with Qt/C++. My approach was to create multiple .h/.cpp files (corresponding to different ui widgets) and call a general .h/.cpp database manager file that handles login, querys, etc. Unfortunately I am kinda stuck because either I get the "QSqlDatabasePrivate::removeDatabase: connection 'databaseName' is still in use, all queries will cease to work." or another kind of odd mistake like 'query.exec: database is not open'.
I've been searching for a day and I can't find anything that helps my case so I decided to try my luck here :)
i'll leave you the database manager and login .h/.cpp files atm:
db.h
#ifndef DB_H
#define DB_H
#include <QtSql>
#include <QSqlDatabase>
#include <QtDebug>
#include <QMessageBox>
class db
{
private:
friend class login;
QSqlDatabase dataBase;
public:
db();
~db();
bool login(QString, QString);
};
#endif // DB_H
db.cpp
#include "db.h"
db::db()
{
dataBase = QSqlDatabase::addDatabase("QSQLITE", "*connection name*");
dataBase.setHostName("*host*");
dataBase.setPassword("*host-password*");
dataBase.setDatabaseName("*connection name");
if(!dataBase.open())
{
//error message
}
else
{
qDebug() << "Open...";
}
}
db::~db()
{
if (dataBase.isOpen())
{
dataBase.close();
}
QSqlDatabase::removeDatabase("*connection name*");
}
login.h
#ifndef LOGIN_H
#define LOGIN_H
#include "db.h"
#include <QDialog>
#include <QDebug>
#include <QMessageBox>
#include <QtSql>
#include <QSqlDatabase>
namespace Ui {
class login;
}
class login : public QDialog
{
Q_OBJECT
public:
explicit login(QWidget *parent = nullptr);
~login();
private slots:
void on_login_button_clicked();
private:
Ui::login *ui;
};
#endif // LOGIN_H
login.cpp -> where the problem is currently happening
#include "login.h"
#include "ui_login.h"
login::login(QWidget *parent) :
QDialog(parent),
ui(new Ui::login)
{
ui->setupUi(this);
//Basic Configurations:
}
login::~login()
{
delete ui;
}
void login::on_login_button_clicked()
{
QString userID = ui->user_insert->text();
QString userPassword = ui->pass_insert->text();
qDebug() << userID << userPassword;
//if conditions to check no one tampers with the limits in lineEdits
else
{
QSqlDatabase loginCall = QSqlDatabase::database("*connection Name*");
if (loginCall.isOpen())
{
qDebug() << "Also Open...";
//call a db.hdb.cpp file to execute the login of the user
}
}
}
I think it's important to say that in the main.cpp I create an instance of the database.
Like I said, I've searched for quite a while and the current method I was trying was calling the current connection with
QSqlDatabase loginCall = QSqlDatabase::database("*connection Name*");
but from here on I can't acess the public funcions of db.h/db.cpp
Can anyone suggest a solution or say what I am doing wrong? If it's stupid bash me for it, it's deserved ahah but I'm quite tired of seeing videos and endless posts and not finding a fix.
Sorry for the long post, thanks in advance!
1 Answer
1
You are using "QSQLITE"
as the driver name when calling QSqlDatabase::addDatabase
as you are trying to use MySQL you should be using "QMYSQL"
instead.
"QSQLITE"
QSqlDatabase::addDatabase
"QMYSQL"
See http://doc.qt.io/qt-5/qsqldatabase.html#addDatabase for a list of driver names.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.