[Qt-interest] QSqlDatabase
Riccardo Roasio
riccardo.roasio at gmail.com
Thu Oct 1 10:15:56 CEST 2009
Hi,
i have a probelm with a QSqlDatabase object, it gives me this warning :
QSqlDatabasePrivate::addDatabase: duplicate connection name
'qt_sql_default_connection', old connection removed.
How can i solve this issue?
The class that use the QSqlDatabase object is this one:
#ifndef MYSQLCONNECTION_H
#define MYSQLCONNECTION_H
#include <QtSql>
#include <QString>
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlError>
#include <QStringList>
#include <QtDebug>
#include <iostream>
#include "logger.h"
class MysqlConnection
{
public:
MysqlConnection(QString host,QString database,QString
username,QString password);
void close();
QSqlQuery query(QString q);
bool opened();
private:
QString username;
QString password;
QString database;
QString host;
QString connection;
QSqlDatabase db;
bool op;
Logger *logger;
};
#endif // MYSQLCONNECTION_H
#include "mysqlconnection.h"
MysqlConnection::MysqlConnection(QString h,QString d,QString u,QString p)
{
std::cout<<"1"<<std::endl;
logger=new Logger();
logger->logOnFile(" MysqlConnection","MysqlConnection","Opening
connection.");
host=h;
database=d;
username=u;
password=p;
logger->logOnFile(" MysqlConnection","MysqlConnection","Adding database.");
db = QSqlDatabase::addDatabase("QMYSQL");
logger->logOnFile(" MysqlConnection","MysqlConnection","Database added.");
db.setHostName("localhost");
db.setDatabaseName("osty");
db.setUserName("root");
db.setPassword("root");
logger->logOnFile(" MysqlConnection","MysqlConnection","Connecting
to database.");
op=db.open();
logger->logOnFile(" MysqlConnection","MysqlConnection","Connected.");
connection=db.connectionName();
logger->logOnFile(" MysqlConnection","MysqlConnection","Connection
name |"+connection+"|.");
logger->logOnFile(" MysqlConnection","MysqlConnection","Connection opened.");
std::cout<<"2"<<std::endl;
}
QSqlQuery MysqlConnection::query(QString q)
{
std::cout<<"3"<<std::endl;
logger->logOnFile(" MysqlConnection","query","Executing query.");
QSqlQuery query_result;
if(op)
{
logger->logOnFile(" MysqlConnection","query","Executing...");
query_result=QSqlQuery(db);
query_result.exec(q);
logger->logOnFile(" MysqlConnection","query","Executed.");
}
else
{
logger->logOnFile(" MysqlConnection","query","Query not
executed. Db not opened.");
}
std::cout<<"4"<<std::endl;
return query_result;
}
void MysqlConnection::close()
{
std::cout<<"5"<<std::endl;
logger->logOnFile(" MysqlConnection","close","Closing connection.");
db.close();
if(op)
{
op=false;
// db.removeDatabase(connection);
}
db.~QSqlDatabase();
logger->logOnFile(" MysqlConnection","close","Connection closed.");
std::cout<<"6"<<std::endl;
}
bool MysqlConnection::opened()
{
return op;
}
More information about the Qt-interest-old
mailing list