Results 1 to 3 of 3

Thread: what is prudent as far as closing connections etc?

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

    Default

    this code works!

    edit works! table grid changes show up in database

    However, I get error on closing the window!:
    Qt Code:
    1. QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    To copy to clipboard, switch view to plain text mode 

    database queries from a client work! What is ceasing to work?

    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include <QMessageBox>
    4. #include <QSqlDatabase>
    5. #include <QSqlError>
    6. #include <QSqlQuery>
    7. #include <QSqlQueryModel>
    8. #include <QSqlTableModel>
    9. int main(int argc, char *argv[])
    10. {
    11. QApplication app(argc, argv);
    12. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    13. db.setHostName("localhost");
    14. db.setDatabaseName("landonx");
    15. db.setUserName("landon4");
    16. db.setPassword("Kateaux3141");
    17. db.setConnectOptions(QString("CLIENT_INTERACTIVE"));
    18. bool ok = db.open();
    19.  
    20. model->setTable( "log_book" );
    21. model->setEditStrategy(QSqlTableModel::OnFieldChange);
    22.  
    23. model->select();
    24. model->setHeaderData( 0, Qt::Horizontal, QObject::tr("rowid") );
    25. model->setHeaderData( 1, Qt::Horizontal, QObject::tr("fdate") );
    26. model->setHeaderData( 2, Qt::Horizontal, QObject::tr("acid") );
    27. model->setHeaderData( 3, Qt::Horizontal, QObject::tr("actype") );
    28. model->setHeaderData( 4, Qt::Horizontal, QObject::tr("nlandings") );
    29. model->setHeaderData( 5, Qt::Horizontal, QObject::tr("nhours") );
    30. QTableView *view = new QTableView();
    31. view->setModel( model );
    32. view->show();
    33.  
    34. // db.close(); keeps edit from working OF COURSE
    35.  
    36. return app.exec();
    37. }
    To copy to clipboard, switch view to plain text mode 

    This is all I can think of at present!

    Still have error message:

    Qt Code:
    1. QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    To copy to clipboard, switch view to plain text mode 




    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include <QMessageBox>
    4. #include <QSqlDatabase>
    5. #include <QSqlError>
    6. #include <QSqlQuery>
    7. #include <QSqlQueryModel>
    8. #include <QSqlTableModel>
    9. #include <QtDebug>
    10. void QWidget::closeEvent(QCloseEvent * e)
    11. {
    12. qDebug()<<"closing";
    13. QSqlDatabase defaultDB = QSqlDatabase::database();
    14. defaultDB.close();
    15. }
    16. int main(int argc, char *argv[])
    17. {
    18. QApplication app(argc, argv);
    19. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    20. db.setHostName("localhost");
    21. db.setDatabaseName("landonx");
    22. db.setUserName("landon4");
    23. db.setPassword("Kateaux3141");
    24. db.setConnectOptions(QString("CLIENT_INTERACTIVE"));
    25. bool ok = db.open();
    26. // QSqlDatabase defaultDB = QSqlDatabase::database();
    27.  
    28. model->setTable( "log_book" );
    29. model->setEditStrategy(QSqlTableModel::OnFieldChange);
    30.  
    31. model->select();
    32. model->setHeaderData( 0, Qt::Horizontal, QObject::tr("rowid") );
    33. model->setHeaderData( 1, Qt::Horizontal, QObject::tr("fdate") );
    34. model->setHeaderData( 2, Qt::Horizontal, QObject::tr("acid") );
    35. model->setHeaderData( 3, Qt::Horizontal, QObject::tr("actype") );
    36. model->setHeaderData( 4, Qt::Horizontal, QObject::tr("nlandings") );
    37. model->setHeaderData( 5, Qt::Horizontal, QObject::tr("nhours") );
    38. QTableView *view = new QTableView();
    39. view->setModel( model );
    40. view->setAlternatingRowColors ( true );
    41. view->setFixedSize ( 800,500 );
    42. view->show();
    43.  
    44. // db.close();
    45.  
    46. return app.exec();
    47. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 3rd December 2009 at 10:46.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: what is prudent as far as closing connections etc?

    You create the model on the heap and you don't destroy it after app.exec() returns. So the "db" gets destroyed first.

    Try:
    Qt Code:
    1. ...
    2. int ret = app.exec();
    3. delete view;
    4. delete model;
    5. return ret;
    To copy to clipboard, switch view to plain text mode 

  3. The following 2 users say thank you to jacek for this useful post:

    akrep55tr (23rd October 2011), landonmkelsey (4th September 2008)

  4. #3
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: what is prudent as far as closing connections etc?

    THAT WORKED! Thanks!

    Some years in the past I had to do something similar!

    Even when one is following a "wrong track", one learns something!

    Perhaps more!
    Last edited by landonmkelsey; 4th September 2008 at 01:55. Reason: added a sentence

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.