What i am trying to do is to make a dialog where the user enters the data needed for a connection and finally makes the connection.

I have two buttons.

The first button should check whether a connection has already been made and if not start a new connection. If a connection has already been made then write a message saying connection already open.

The second button should disconnect the database.

The problem is that I cannot disconnect the database and that instead of checking whether one is already open it creates another one each time i ress the button. (and I receive the relevant error code form the command line).

I have attached the code. I cant figure out what's wrong on my own.Your help is much appreciated.

Many thanks in advance.

Qt Code:
  1. #include "database.h"
  2.  
  3. #include <QtGui>
  4. #include <QApplication>
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. QApplication a(argc, argv);
  9. database w;
  10. w.show();
  11. return a.exec();
  12. }
To copy to clipboard, switch view to plain text mode 



Qt Code:
  1. #ifndef DATABASE_H
  2. #define DATABASE_H
  3.  
  4. #include <QtGui/QWidget>
  5. #include <QtSql>
  6. #include "ui_database.h"
  7. class A {
  8. public:
  9. QString pinakas;
  10. QString onoma;
  11. QString kodikos;
  12. };
  13.  
  14. class database : public QWidget
  15. {
  16. Q_OBJECT
  17.  
  18. public:
  19. database(QWidget *parent = 0);
  20. ~database();
  21.  
  22.  
  23. public slots:
  24. void sindesou();
  25. void aposindesou();
  26.  
  27. private:
  28. Ui::databaseClass ui;
  29. A ena;
  30.  
  31. };
  32.  
  33. #endif // DATABASE_H
To copy to clipboard, switch view to plain text mode 

database.cpp:
Qt Code:
  1. #include "database.h"
  2. #include <QtSql>
  3. #include <QMessageBox>
  4. #include <QSqlDatabase>
  5. #include <QSqlError>
  6. #include <QSqlQuery>
  7. #include <QApplication>
  8. #include <QCoreApplication>
  9. #include <QDebug>
  10. #include "ui_database.h"
  11.  
  12.  
  13.  
  14. database::database(QWidget *parent)
  15. : QWidget(parent)
  16. {
  17. ui.setupUi(this);
  18. connect(ui.sindesi, SIGNAL(clicked()), this, SLOT(sindesou()));
  19. connect(ui.aposindesi, SIGNAL(clicked()), this, SLOT(aposindesou()));
  20.  
  21. //QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
  22. }
  23.  
  24. void database::sindesou() {
  25. ena.pinakas = ui.pinakas->text();
  26. ena.onoma = ui.onoma->text();
  27. ena.kodikos = ui.kodikos->text();
  28. if (!db.open()) {
  29. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
  30. db.setDatabaseName(ena.pinakas);
  31. db.setUserName(ena.onoma);
  32. db.setPassword(ena.kodikos);
  33. db.setHostName("127.0.0.1");
  34. db.setPort( 3306 );
  35. if (!db.open()) {
  36. ui.katastasi->setText("Not connected");
  37. } else {
  38. ui.katastasi->setText("connected");
  39. }
  40. }
  41. else {
  42. ui.katastasi->setText("It has already been connected");
  43. }
  44. };
  45.  
  46.  
  47. void database::aposindesou() {
  48. db.close();///<<--- This doesnt close the connection
  49. }
  50.  
  51. database::~database()
  52. {
  53.  
  54. }
To copy to clipboard, switch view to plain text mode