Results 1 to 6 of 6

Thread: How remove database in right way

  1. #1
    Join Date
    Jul 2016
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default How remove database in right way

    Hi everybody, when I run my program, I've got this message:
    QSqlDatabasePrivate removeDatabase connection qt_sql_default_connection is still in use all queries will cease to work.

    this is my class for connection:

    ConnectDB.h
    Qt Code:
    1. #ifndef CONNECTDB_H
    2. #define CONNECTDB_H
    3.  
    4. #include <QString>
    5. #include <QSqlDatabase>
    6.  
    7. class ConnectDB
    8. {
    9. public:
    10. ConnectDB(const QString sHostName, const QString sDatabaseName, const QString sUserName, const QString sPassword, int iPort, const QString connectionName);
    11. ~ConnectDB();
    12. bool openConnection();
    13. void closeConnection();
    14.  
    15. private:
    16. QString sHostName, sDatabaseName, sUserName, sPassword, sConnectionName;
    17. int iPort;
    18. };
    19.  
    20. #endif // CONNECTDB_H
    To copy to clipboard, switch view to plain text mode 

    ConnectDB.cpp
    Qt Code:
    1. #include "connectdb.h"
    2. #include <QString>
    3. #include <QSqlDatabase>
    4. #include <QSqlError>
    5. #include <QDebug>
    6.  
    7. ConnectDB::ConnectDB(const QString sHostName, const QString sDatabaseName, const QString sUserName, const QString sPassword, int iPort, const QString connectionName)
    8. {
    9. this->sHostName = sHostName;
    10. this->sDatabaseName = sDatabaseName;
    11. this->sUserName = sUserName;
    12. this->sPassword = sPassword;
    13. this->iPort = iPort;
    14. this->sConnectionName = connectionName;
    15.  
    16. db = new QSqlDatabase(QSqlDatabase::addDatabase("QPSQL",sConnectionName));
    17. db->setHostName(sHostName);
    18. db->setDatabaseName(sDatabaseName);
    19. db->setUserName(sUserName);
    20. db->setPassword(sPassword);
    21. db->setPort(iPort);
    22. qDebug() << "Openning...";
    23. }
    24.  
    25. bool ConnectDB::openConnection()
    26. {
    27. if(!db->open())
    28. {
    29. qDebug() << db->lastError().text();
    30. return false;
    31. }
    32. return true;
    33. }
    34.  
    35. void ConnectDB::closeConnection()
    36. {
    37. if (db->isOpen() && db->isValid()) {
    38. db->close();
    39. qDebug() << "Closing ...";
    40. QStringList lsConnectionName = QSqlDatabase::connectionNames();
    41. for (int i = 0; i < lsConnectionName.count(); ++i) {
    42. qDebug() << "CONEXION: " << lsConnectionName[i];
    43. QSqlDatabase::removeDatabase(lsConnectionName[i]);
    44. }
    45. }
    46. }
    47.  
    48. ConnectDB::~ConnectDB()
    49. {
    50. delete db;
    51. }
    To copy to clipboard, switch view to plain text mode 

    So, this is the class where i use class ConnectDB:
    frmmantenimiento.h
    Qt Code:
    1. #ifndef FRMMANTENIMIENTO_H
    2. #define FRMMANTENIMIENTO_H
    3.  
    4. #include <QWidget>
    5.  
    6. class QLabel;
    7. class QLineEdit;
    8. class QSqlQuery;
    9.  
    10. class FrmMantenimiento : public QWidget
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit FrmMantenimiento(const QString strUserName, const QString strTableName, QWidget *parent = 0);
    15. ~FrmMantenimiento();
    16. void setUserName(const QString strUserName);
    17. QString getUserName();
    18. void setTableName(const QString strTableName);
    19. QString getTableName();
    20. void setEnableInput();
    21. void setDisableInput();
    22. void setFirstStatusDMLButtons();
    23. void setSecondStatusDMLButtons();
    24. void loadData();
    25.  
    26. private:
    27. QLabel *lbCode;
    28. QLineEdit *leCode;
    29. QLabel *lbDescription;
    30. QLineEdit *leDescription;
    31. QPushButton *pbNew;
    32. QPushButton *pbEdit;
    33. QPushButton *pbSave;
    34. QPushButton *pbDelete;
    35. QPushButton *pbFind;
    36. QPushButton *pbClose;
    37. QGridLayout *glInput;
    38. QVBoxLayout *vlInput;
    39. QVBoxLayout *vlDML;
    40. QHBoxLayout *hlMantenimiento;
    41. QString strUserName;
    42. QString strTableName;
    43.  
    44. signals:
    45.  
    46. private slots:
    47. void pbNewClicked();
    48. void pbEditClicked();
    49. void pbSaveClicked();
    50. void pbDeleteClicked();
    51. void actionDML(int iStatusDML);
    52. };
    53.  
    54. #endif // FRMMANTENIMIENTO_H
    To copy to clipboard, switch view to plain text mode 

    and this is the implementation frmmantenimiento.cpp
    Qt Code:
    1. #include "frmmantenimiento.h"
    2. #include "connectdb.h"
    3.  
    4. #include <QLabel>
    5. #include <QLineEdit>
    6. #include <QPushButton>
    7. #include <QGridLayout>
    8. #include <QHBoxLayout>
    9. #include <QVBoxLayout>
    10. #include <QSqlQuery>
    11. #include <QSqlError>
    12. #include <QMessageBox>
    13. #include <QCloseEvent>
    14. #include <QDebug>
    15.  
    16. int iStatusDML = 9;
    17.  
    18. FrmMantenimiento::FrmMantenimiento(const QString strUserName, const QString strTableName, QWidget *parent) : QWidget(parent)
    19. {
    20. lbCode = new QLabel("Codigo");
    21. leCode = new QLineEdit;
    22. leCode->setFixedWidth(30);
    23. lbDescription = new QLabel("Descripcion");
    24. leDescription = new QLineEdit;
    25. leDescription->setFixedWidth(200);
    26.  
    27. pbNew = new QPushButton("Nuevo");
    28. pbNew->setIcon(QIcon("../images/new_48.png"));
    29. pbEdit = new QPushButton("Editar");
    30. pbEdit->setIcon(QIcon("../images/edit_48.png"));
    31. pbSave = new QPushButton("Guardar");
    32. pbSave->setIcon(QIcon("../images/save_48.png"));
    33. pbDelete = new QPushButton("Eliminar");
    34. pbDelete->setIcon(QIcon("../images/delete_48.png"));
    35. pbFind = new QPushButton("Buscar");
    36. pbFind->setIcon(QIcon("../images/find_48.png"));
    37. pbClose = new QPushButton("Cerrar");
    38. pbClose->setIcon(QIcon("../images/exit_48.png"));
    39.  
    40. glInput = new QGridLayout;
    41. glInput->addWidget(lbCode, 0, 0);
    42. glInput->addWidget(leCode, 0, 1);
    43. glInput->addWidget(lbDescription, 1, 0);
    44. glInput->addWidget(leDescription, 1, 1);
    45.  
    46. vlInput = new QVBoxLayout;
    47. vlInput->addLayout(glInput);
    48. vlInput->addStretch();
    49.  
    50. vlDML = new QVBoxLayout;
    51. vlDML->addWidget(pbNew);
    52. vlDML->addWidget(pbEdit);
    53. vlDML->addWidget(pbSave);
    54. vlDML->addWidget(pbDelete);
    55. vlDML->addStretch();
    56. vlDML->addWidget(pbFind);
    57. vlDML->addStretch();
    58. vlDML->addWidget(pbClose);
    59.  
    60. hlMantenimiento = new QHBoxLayout;
    61. hlMantenimiento->addLayout(vlInput);
    62. hlMantenimiento->addSpacing(20);
    63. hlMantenimiento->addLayout(vlDML);
    64.  
    65. this->setLayout(hlMantenimiento);
    66. this->setWindowTitle("Tabla[" + getUserName() + "]");
    67.  
    68. setUserName(strUserName);
    69. setTableName(strTableName);
    70. setDisableInput();
    71. setFirstStatusDMLButtons();
    72.  
    73. loadData();
    74.  
    75. connect(pbNew, SIGNAL(clicked(bool)), this, SLOT(pbNewClicked()));
    76. connect(pbEdit, SIGNAL(clicked(bool)), this, SLOT(pbEditClicked()));
    77. connect(pbSave, SIGNAL(clicked(bool)), this, SLOT(pbSaveClicked()));
    78. connect(pbDelete, SIGNAL(clicked(bool)), this, SLOT(pbDeleteClicked()));
    79. connect(pbClose, SIGNAL(clicked(bool)), this, SLOT(close()));
    80.  
    81.  
    82. this->setAttribute(Qt::WA_DeleteOnClose);
    83.  
    84.  
    85. }
    86.  
    87. void FrmMantenimiento::loadData()
    88. {
    89. ConnectDB *wlfConnectDB = new ConnectDB("localhost","zcdevelopment","cp21021976","cp21021976",5432, "cnSelect");
    90. wlfConnectDB->openConnection();
    91.  
    92. QSqlQuery *wlfQuery = new QSqlQuery;
    93.  
    94. wlfQuery->prepare("SELECT sexo_codigo, sexo_descripcion FROM cp21021976.tb_sexo ORDER BY sexo_codigo");
    95. wlfQuery->exec();
    96.  
    97. wlfQuery->seek(0);
    98. QString strCodigo = wlfQuery->value(0).toString();
    99. QString strDescripcion = wlfQuery->value(1).toString();
    100. leCode->setText(strCodigo);
    101. leDescription->setText(strDescripcion);
    102.  
    103. wlfConnectDB->closeConnection();
    104. }
    To copy to clipboard, switch view to plain text mode 

    I don't know how to resolve this problem...

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How remove database in right way

    More than likely, you have a QSqlQuery that is still active. When you're done with your QSqlQuery items, you should do a QSqlQuery::finish().

    Edit: I didn't study your code thoroughly, but are you leaking code in frmmantenimiento.cpp on lines 89 and 92?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Jul 2016
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How remove database in right way

    Sorry jefftee, but, i don't understand, that's all my code.

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How remove database in right way

    Quote Originally Posted by Tarko1976 View Post
    Sorry jefftee, but, i don't understand, that's all my code.
    I understand it's all your code, but if you wrote that code, you should understand my reply...

    Add a
    Qt Code:
    1. wlfQuery->finish();
    To copy to clipboard, switch view to plain text mode 
    before line 103. To see if that's the cause of your connection still in use, as well as fix the memory leak below...

    You also allocate your connection and query objects on the heap ("new" keyword) but you don't free that memory anywhere, so you should just allocate them on the stack and they'll go out of scope at the end of your loadData() method or make them class variables where they can be used in other parts of your code if needed.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  5. #5
    Join Date
    Jul 2016
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How remove database in right way

    Hi jefftee, i added:
    Qt Code:
    1. wlfQuery->finish();
    To copy to clipboard, switch view to plain text mode 

    but, i got the same message, i wrote in loadData() method:
    Qt Code:
    1. delete wlfQuery;
    2. delete wlfConnectDB;
    To copy to clipboard, switch view to plain text mode 

    and that's not work.

  6. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How remove database in right way

    Please re-post your code as tested with the additions in your last post.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Replies: 2
    Last Post: 6th April 2014, 11:07
  2. QSQLITE database changes not showing up in database
    By Cyrebo in forum Qt Programming
    Replies: 6
    Last Post: 14th April 2013, 23:18
  3. Replies: 2
    Last Post: 27th August 2012, 03:27
  4. [SOLVED] database opened .. database not open
    By kapitanluffy in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2011, 10:39
  5. Replies: 9
    Last Post: 20th May 2010, 09:55

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
  •  
Qt is a trademark of The Qt Company.