Results 1 to 4 of 4

Thread: QSqlDatabase coneciton removal!

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

    Default QSqlDatabase coneciton removal!

    Hi all,

    In my program i have a main class (kentrko) which creates a QSqlDatabase* object. I want to pass this object to a dialogue to detect whether the connection is opened or closed and if not to open it. The code works fine and compiles but i receive the following error when I am opening the dialogue window again:
    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    kentriko class:
    Qt Code:
    1. #include "kentriko.h"
    2. #include <QMdiSubWindow>
    3.  
    4. kentriko::kentriko(QWidget *parent)
    5. : QMainWindow(parent)
    6. {
    7. ui.setupUi(this);
    8. connect(ui.actionAnalisi, SIGNAL(triggered()), this, SLOT(anixe_analisi()));
    9. connect(ui.actionSindesi, SIGNAL(triggered()), this, SLOT(anixe_sindesi()));
    10. *db = QSqlDatabase::addDatabase("QMYSQL");
    11. }
    12.  
    13. void kentriko::anixe_sindesi(){
    14. sindesi *sindesi1= new sindesi(0,db) ;
    15. sindesi1->show();
    16. }
    To copy to clipboard, switch view to plain text mode 

    and the kentriko header:
    Qt Code:
    1. #ifndef KENTRIKO_H
    2. #define KENTRIKO_H
    3. #include <QtGui/QMainWindow>
    4. #include "ui_kentriko.h"
    5. #include <QtSql>
    6. #include "../parathrira/sindesi/sindesi.h"// i klasi silogi ine orismeni apo tin sindesi giayto xreazete edo to header
    7. #include "../parathrira/analisi/analisi.h"
    8. class kentriko : public QMainWindow
    9. {
    10. Q_OBJECT
    11. public:
    12. kentriko(QWidget *parent = 0);
    13. ~kentriko();
    14. public slots:
    15. void anixe_analisi ();
    16. void anixe_sindesi ();
    17. };
    To copy to clipboard, switch view to plain text mode 

    the dialog that i want to pass the db and manipulate it is:

    Qt Code:
    1. #ifndef SINDESI_H
    2. #define SINDESI_H
    3. #include <QtSql>
    4. #include <QtGui/QDialog>
    5. #include "ui_sindesi.h"
    6.  
    7. class sindesi : public QDialog
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. sindesi(QWidget *parent , QSqlDatabase* db);
    13. ~sindesi();
    14. public slots:
    15. void sindesou();
    16. void aposindesou();
    17.  
    18. private:
    19. Ui::sindesiClass ui;
    20. };
    21.  
    22. #endif // SINDESI_H
    To copy to clipboard, switch view to plain text mode 

    and the implementation:
    Qt Code:
    1. #include "sindesi.h"
    2. #include <QtSql>
    3. sindesi::sindesi(QWidget *parent,QSqlDatabase* db)
    4. : QDialog(parent)
    5. {
    6. db3=db;
    7. ui.setupUi(this);
    8. connect(ui.sindesi, SIGNAL(clicked()), this, SLOT(sindesou()));
    9. connect(ui.aposindesi, SIGNAL(clicked()), this, SLOT(aposindesou()));
    10. }
    11. void sindesi::sindesou() {
    12. if (db3->isOpen())
    13. ui.pliroforia_2->setText("ine idi anixti");
    14. if (!db3->isOpen()) {
    15. db3->setDatabaseName(ui.pinakas->text());
    16. db3->setUserName(ui.onoma->text());
    17. db3->setPassword(ui.kodikos->text());
    18. db3->setHostName("127.0.0.1");
    19. db3->setPort(3306);
    20. if (!db3->open()) {
    21. ui.katastasi->setText("Not connected");
    22. } else {
    23. ui.katastasi->setText("connected");
    24. }
    25. } else {
    26. ui.katastasi->setText("It has already been connected");
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 
    Any ideas why is this happening?

    Many thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlDatabase coneciton removal!

    There is no point in passing the database object around. You can always retrieve it using QSqlDatabase::database().

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlDatabase coneciton removal!

    Error causes:
    1. you are trying to create a connection while one of the same name is still existing
    2. you are trying to delete a QDatabase while it is still in use


    Are you trying to delete the connection (perhaps in ~sindesi)?
    You can only remove the connection once all its user have stopped using it (e.g. been destroyed).

    Show us the rest of your code. The code shown looks ok.
    Most likely you create two instances of the kentriko class or something like that.

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

    Default Re: QSqlDatabase coneciton removal!

    I have attached the full code.

    I have now modified it and found something strange. I pass the db object to the dialogue and then copy it to another object so it can be visibe to the rest of the class. By manipulating the copy of the objcect is like manipulating the original object which cant be true in c++.

    in my example i use pointers but the result would have been exactly the same if i have passed the object to the sindesi object and then coppied to another object which is visible to the entire class.

    I found in the documentation :
    To make programming more convenient, QSqlDatabase is a value class. Any changes done to a database connection through one QSqlDatabase object will affect other QSqlDatabase objects representing the same connection.
    I dont know if that expllains the fact that when i am coping the QSqlDatabase object and using this copy is like i am using the original.

    to demonstarte that i will show the two versions of the code which shouldnt be equivalnet but they are:

    the pointers version:
    Qt Code:
    1. kentriko class implementation:
    2. #include "kentriko.h"
    3. #include <QMdiSubWindow>
    4.  
    5. kentriko::kentriko(QWidget *parent)
    6. : QMainWindow(parent)
    7. {
    8. ui.setupUi(this);
    9. connect(ui.actionAnalisi, SIGNAL(triggered()), this, SLOT(anixe_analisi()));
    10. connect(ui.actionSindesi, SIGNAL(triggered()), this, SLOT(anixe_sindesi()));
    11. db = QSqlDatabase::addDatabase("QMYSQL");
    12. }
    13. void kentriko::anixe_sindesi(){
    14. sindesi *sindesi1 = new sindesi(0,&db);
    15. sindesi1->show();
    16. }
    17.  
    18. and the header file:
    19. #ifndef KENTRIKO_H
    20. #define KENTRIKO_H
    21.  
    22. #include <QtGui/QMainWindow>
    23. #include "ui_kentriko.h"
    24. #include <QtSql>
    25. #include "../parathrira/sindesi/sindesi.h"// i klasi silogi ine orismeni apo tin sindesi giayto xreazete edo to header
    26. #include "../parathrira/analisi/analisi.h"
    27.  
    28. class kentriko : public QMainWindow
    29. {
    30. Q_OBJECT
    31.  
    32. public:
    33. kentriko(QWidget *parent = 0);
    34. ~kentriko();
    35.  
    36. public slots:
    37. void anixe_analisi ();
    38. void anixe_sindesi ();
    39.  
    40. private:
    41. Ui::kentrikoClass ui;
    42. };
    To copy to clipboard, switch view to plain text mode 

    and the dialogue class:
    Qt Code:
    1. #include "sindesi.h"
    2. #include <QtSql>
    3. sindesi::sindesi(QWidget *parent,QSqlDatabase* db)
    4. : QDialog(parent)
    5. {
    6. db5= db;// in thsi case i pass the pointer
    7. ui.setupUi(this);
    8. connect(ui.sindesi, SIGNAL(clicked()), this, SLOT(sindesou()));
    9. connect(ui.aposindesi, SIGNAL(clicked()), this, SLOT(aposindesou()));
    10. }
    11. void sindesi::sindesou() {
    12. if (!db5->isOpen()) {
    13. db5->setDatabaseName(ui.pinakas->text());
    14. db5->setUserName(ui.onoma->text());
    15. db5->setPassword(ui.kodikos->text());
    16. db5->setHostName("127.0.0.1");
    17. db5->setPort(3306);
    18. if (!db5->open()) {
    19. ui.katastasi->setText("not connected");
    20. } else {
    21. ui.katastasi->setText("connected");
    22. }
    23. } else {
    24. ui.katastasi->setText("it has already been connected");
    25. }
    26. }
    27.  
    28. and the header:
    29. #ifndef SINDESI_H
    30. #define SINDESI_H
    31. #include <QtSql>
    32. #include <QtGui/QDialog>
    33. #include "ui_sindesi.h"
    34.  
    35. class sindesi : public QDialog
    36. {
    37. Q_OBJECT
    38.  
    39. public:
    40. sindesi(QWidget *parent,QSqlDatabase *db);
    41. ~sindesi();
    42. public slots:
    43. void sindesou();
    44. void aposindesou();
    45.  
    46. private:
    47. Ui::sindesiClass ui;
    48. };
    To copy to clipboard, switch view to plain text mode 

    and below the version where the whole object is passed:
    kentriko class implementation:
    Qt Code:
    1. #include "kentriko.h"
    2. #include <QMdiSubWindow>
    3.  
    4. kentriko::kentriko(QWidget *parent)
    5. : QMainWindow(parent)
    6. {
    7. ui.setupUi(this);
    8. connect(ui.actionAnalisi, SIGNAL(triggered()), this, SLOT(anixe_analisi()));
    9. connect(ui.actionSindesi, SIGNAL(triggered()), this, SLOT(anixe_sindesi()));
    10. db = QSqlDatabase::addDatabase("QMYSQL");
    11. }
    12. void kentriko::anixe_sindesi(){
    13. sindesi *sindesi1 = new sindesi(0,&db);
    14. sindesi1->show();
    15. }
    16.  
    17. and the header file:
    18. #ifndef KENTRIKO_H
    19. #define KENTRIKO_H
    20.  
    21. #include <QtGui/QMainWindow>
    22. #include "ui_kentriko.h"
    23. #include <QtSql>
    24. #include "../parathrira/sindesi/sindesi.h"// i klasi silogi ine orismeni apo tin sindesi giayto xreazete edo to header
    25. #include "../parathrira/analisi/analisi.h"
    26.  
    27. class kentriko : public QMainWindow
    28. {
    29. Q_OBJECT
    30.  
    31. public:
    32. kentriko(QWidget *parent = 0);
    33. ~kentriko();
    34.  
    35. public slots:
    36. void anixe_analisi ();
    37. void anixe_sindesi ();
    38.  
    39. private:
    40. Ui::kentrikoClass ui;
    41. };
    To copy to clipboard, switch view to plain text mode 

    and the dialogue class:
    Qt Code:
    1. #include "sindesi.h"
    2. #include <QtSql>
    3. sindesi::sindesi(QWidget *parent,QSqlDatabase db)
    4. : QDialog(parent)
    5. {
    6. db5= db;// here i pass and copy the whole object and via the db5 the db is still accesible
    7. ui.setupUi(this);
    8. connect(ui.sindesi, SIGNAL(clicked()), this, SLOT(sindesou()));
    9. connect(ui.aposindesi, SIGNAL(clicked()), this, SLOT(aposindesou()));
    10. }
    11. void sindesi::sindesou() {
    12. if (!db5.isOpen()) {
    13. db5.setDatabaseName(ui.pinakas->text());
    14. db5.setUserName(ui.onoma->text());
    15. db5.setPassword(ui.kodikos->text());
    16. db5.setHostName("127.0.0.1");
    17. db5.setPort(3306);
    18. if (!db5.open()) {
    19. ui.katastasi->setText("not connected");
    20. } else {
    21. ui.katastasi->setText("connected");
    22. }
    23. } else {
    24. ui.katastasi->setText("it has already been connected");
    25. }
    26. }
    27.  
    28. and the header:
    29. #ifndef SINDESI_H
    30. #define SINDESI_H
    31. #include <QtSql>
    32. #include <QtGui/QDialog>
    33. #include "ui_sindesi.h"
    34.  
    35. class sindesi : public QDialog
    36. {
    37. Q_OBJECT
    38.  
    39. public:
    40. sindesi(QWidget *parent,QSqlDatabase db);
    41. ~sindesi();
    42. public slots:
    43. void sindesou();
    44. void aposindesou();
    45.  
    46. private:
    47. Ui::sindesiClass ui;
    48. };
    To copy to clipboard, switch view to plain text mode 

    I am not sure if that supposed to be correct but in both cases works exactly the same.

    Many thanks in advance.
    Regards
    Last edited by cbarmpar; 27th September 2008 at 11:37.

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.