Results 1 to 2 of 2

Thread: SQL connection closure problem.

  1. #1
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default SQL connection closure problem.

    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 

  2. #2
    Join Date
    Jun 2008
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQL connection closure problem.

    Hello,

    you create a local object of QDatabase in line 29 of database.cpp (sindesou) and open this local object.

    In aposindesou you close the QDatabase object of the class databse. But this object isn't open. You can't close it.

    Maybe you should use the class database object in line 29 of database.cpp (sindesou).

    Qt Code:
    1. db = QSqlDatabase::addDatabase("QMYSQL");
    To copy to clipboard, switch view to plain text mode 

    bye

  3. The following user says thank you to mummy for this useful post:

    cbarmpar (8th September 2008)

Similar Threads

  1. DB Connection Problem
    By ktmdwn in forum Qt Programming
    Replies: 13
    Last Post: 4th August 2010, 15:12
  2. Client/Server Error: BadIDChoice
    By 3nc31 in forum Qt Programming
    Replies: 5
    Last Post: 27th November 2007, 10:22
  3. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 09:52
  4. SQL problem
    By nimmyj in forum Qt Programming
    Replies: 4
    Last Post: 25th December 2006, 18:43
  5. Connection with MS SQL
    By manish_pesit in forum Qt Programming
    Replies: 4
    Last Post: 13th September 2006, 07:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.