You cannot use the QSqlQuery to pass data back to other parts of the program if you insist on closing the database connection (even if it let you). It probably won't let you close the database connection because the QSqlQuery you create and execute is still active.

Why do you want to keep opening and closing the connection rather than keeping a persistent connection for the life of your sql_functions instance?

How about something like (untested - not at my machine):
Qt Code:
  1. #include "sql_functions.hpp"
  2.  
  3. sql_functions::sql_functions( const QString database ):
  4. m_database(database)
  5. {
  6. // Establish the database connection
  7. QSQLDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
  8. db.setDatabaseName( m_database );
  9. db.open();
  10.  
  11. /**
  12.   * Create structure :
  13.   * - to store data for SQL request
  14.   * - to store data for statistics
  15.   **/
  16. s_req = new Struct_Request;
  17. s_stats = new Struct_Statistic;
  18. }
  19.  
  20. sql_functions::~sql_functions()
  21. {
  22. delete s_req;
  23. delete s_stats;
  24.  
  25. QString connectionName;
  26. { // scope the db instance so that removeDatabase does not complain
  27. // about active connections.
  28. QSqlDatabase db = QSqlDatabase::database();
  29. connectionname = db.connectionName();
  30. if (db.isOpen())
  31. db.close();
  32. }
  33. QSqlDatabase::removeDatabase(connectionName);
  34.  
  35. }
  36.  
  37. void sql_functions::exec( const int request_type, QString movie_title,
  38. QString type, QString borrower_name,
  39. QString Id_movie_update )
  40. {
  41. /** Clear data field in request structure **/
  42. s_req->data.clear();
  43.  
  44. QSqlDatabase db = QSqlDatabase::database();
  45.  
  46. /** If no error occured, SQL request is executed **/
  47. if ( db.open() )
  48. {
  49. s_req->data = QSqlQuery( db );
  50. switch( request_type )
  51. {
  52. case RequestSQL::DISPLAY_ALL :
  53. s_req->data.exec( "SELECT type,titre,dvd_en_pret,bluray_en_pret,dvd_prete_a,bluray_prete_a,est_combo "
  54. "FROM films ORDER BY titre COLLATE NOCASE;" );
  55. break;
  56.  
  57. case RequestSQL::DISPLAY_DVD :
  58. s_req->data.exec( "SELECT type,titre,dvd_en_pret,bluray_en_pret,dvd_prete_a,bluray_prete_a,est_combo
  59. "FROM films WHERE type IN ('DVD') ORDER BY titre COLLATE NOCASE;" );
  60. break;
  61.  
  62. case RequestSQL::DISPLAY_BLURAY :
  63. s_req->data.exec( "SELECT type,titre,dvd_en_pret,bluray_en_pret,dvd_prete_a,bluray_prete_a,est_combo "
  64. "FROM films WHERE type IN ('BLURAY') ORDER BY titre COLLATE NOCASE;" );
  65. break;
  66.  
  67. default:
  68. break;
  69. }
  70.  
  71. }
  72. }
To copy to clipboard, switch view to plain text mode 
no m_db, no first_call, no constant open/close.