Hi everyone,
I'm here to provide you some fun
Take this code and paste it in a QPushButton::clicked() slot
mydb db1;
if(db1.open())
QMessageBox::information(this,
"DEBUG info",
"Connected");
db1.close();
mydb db1;
if(db1.open())
QMessageBox::information(this,"DEBUG info","Connected");
db1.close();
To copy to clipboard, switch view to plain text mode
then paste this somewhere in the .h or .cpp as you prefer
class mydb{
private:
QVector<QSqlDatabase*> *handle;
public:
mydb(){ handle=new QVector<QSqlDatabase*>(); }
~mydb(){ delete handle; }
bool open()
{
handle.setHostName("host");
handle.setDatabaseName("db");
handle.setUserName("username");
handle.setPassword("password");
handle.open();
this->handle->append(&handle);
return handle.isOpen();
}
void close()
{
if(h->isValid())
qDebug() << "Valid";
if(h->isOpen())
qDebug() << "Open";
h->close();
handle->remove(0);
}
};
class mydb{
private:
QVector<QSqlDatabase*> *handle;
public:
mydb(){ handle=new QVector<QSqlDatabase*>(); }
~mydb(){ delete handle; }
bool open()
{
QSqlDatabase handle=QSqlDatabase::addDatabase("QMYSQL","connName");
handle.setHostName("host");
handle.setDatabaseName("db");
handle.setUserName("username");
handle.setPassword("password");
handle.open();
this->handle->append(&handle);
return handle.isOpen();
}
void close()
{
QSqlDatabase *h=handle->at(0);
if(h->isValid())
qDebug() << "Valid";
if(h->isOpen())
qDebug() << "Open";
h->close();
handle->remove(0);
}
};
To copy to clipboard, switch view to plain text mode
includes
#include <QtGui>
#include <QtSql>
// these are enhough, don't forget QT+=sql in file .pro
#include <QtGui>
#include <QtSql>
// these are enhough, don't forget QT+=sql in file .pro
To copy to clipboard, switch view to plain text mode
I'm compiling with mingw and running under Windows Xp SP3.
To me this code runs clearly in release and raises exception in debug.
Switching between modes with QtCreator causes it to change behavior.
I see nothing wrong in code... maybe you can notice something I'm missing.
Have fun
Bookmarks