Hi all!
I have the following code:
#ifndef QMYSQLCONNECTION_H
#define QMYSQLCONNECTION_H
#include <QObject>
#include <QSqlDatabase>
{
Q_OBJECT
friend class QMysqlTable;
public:
QMysqlConnection(const QString& host, const QString& user, const QString&
bool isOpen(void) const;
void close(void);
};
#endif // QMYSQLCONNECTION_H
#ifndef QMYSQLCONNECTION_H
#define QMYSQLCONNECTION_H
#include <QObject>
#include <QSqlDatabase>
class QMysqlConnection: public QObject, protected QSqlDatabase
{
Q_OBJECT
friend class QMysqlTable;
public:
QMysqlConnection(const QString& host, const QString& user, const QString&
password, const QString database, QObject* = 0);
bool isOpen(void) const;
void close(void);
};
#endif // QMYSQLCONNECTION_H
To copy to clipboard, switch view to plain text mode
#include <QDebug>
#include <QSqlError>
#include "QMysqlConnection.h"
// Public functions
QMysqlConnection::QMysqlConnection(const QString& host, const QString& user,
{
this->addDatabase("QMYSQL");
this->setHostName(host);
this->setUserName(user);
this->setPassword(password);
this->setDatabaseName(database);
this->open();
qDebug() << "Last connection error = " << this->lastError();
}
bool QMysqlConnection::isOpen(void) const
{
}
void QMysqlConnection::close(void)
{
}
#include <QDebug>
#include <QSqlError>
#include "QMysqlConnection.h"
// Public functions
QMysqlConnection::QMysqlConnection(const QString& host, const QString& user,
const QString& password, const QString database, QObject* parent):
QObject(parent)
{
this->addDatabase("QMYSQL");
this->setHostName(host);
this->setUserName(user);
this->setPassword(password);
this->setDatabaseName(database);
this->open();
qDebug() << "Last connection error = " << this->lastError();
}
bool QMysqlConnection::isOpen(void) const
{
return QSqlDatabase::isOpen();
}
void QMysqlConnection::close(void)
{
QSqlDatabase::close();
}
To copy to clipboard, switch view to plain text mode
This returns me the following error:
QSqlError(-1, "Driver not loaded", "Driver not loaded")
But if I change this code
this->addDatabase("QMYSQL");
this->setHostName(host);
this->setUserName(user);
this->setPassword(password);
this->setDatabaseName(database);
this->open();
this->addDatabase("QMYSQL");
this->setHostName(host);
this->setUserName(user);
this->setPassword(password);
this->setDatabaseName(database);
this->open();
To copy to clipboard, switch view to plain text mode
to this code:
db.setHostName(host);
db.setUserName(user);
db.setPassword(password);
db.setDatabaseName(database);
db.open();
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(host);
db.setUserName(user);
db.setPassword(password);
db.setDatabaseName(database);
db.open();
To copy to clipboard, switch view to plain text mode
it works! Why this code works and the other code not?
Bookmarks