I use the typical connection.h:
#ifndef CONNECTION_H
#define CONNECTION_H
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
db.setHostName(newHostName);
db.setDatabaseName(newDatabase);
db.setUserName(newUserName);
db.setPassword(newPassword);
if (!db.open()) {
qApp->trUtf8("Error message\n"
""
return false;
}
return true;
}
#endif
#ifndef CONNECTION_H
#define CONNECTION_H
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
static bool createConnection(const QString newHostName, const QString newDatabase, const QString newUserName, const QString newPassword){
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(newHostName);
db.setDatabaseName(newDatabase);
db.setUserName(newUserName);
db.setPassword(newPassword);
if (!db.open()) {
QMessageBox::critical(0, qApp->trUtf8("ERROR!"),
qApp->trUtf8("Error message\n"
""
"info..."), QMessageBox::Cancel);
return false;
}
return true;
}
#endif
To copy to clipboard, switch view to plain text mode
One question about this.
Every time i reconnect i get a
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
To copy to clipboard, switch view to plain text mode
Is there a problem with this?
Bookmarks