Results 1 to 16 of 16

Thread: Load a new Sqlite database problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Unhappy Re: Load a new Sqlite database problem

    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.

  2. #2
    Join Date
    Jan 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Load a new Sqlite database problem

    Thanks for your answer. I update my code.

    No compilation problem, but i have an error when i closing my program, here :
    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 

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Load a new Sqlite database problem

    Where else are you creating queries against the database? Are you creating copies of the query created by this class elsewhere in the program? Are they being cleared or deleted before this point?

  4. #4
    Join Date
    Jan 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Load a new Sqlite database problem

    Well,
    All my SQL querries are create in function
    Qt Code:
    1. void sql_functions::exec
    To copy to clipboard, switch view to plain text mode 

    Querries result are store in a structure, and others function can used this value to get back request values with
    Qt Code:
    1. QString result;
    2. while s_req->data.next()
    3. result = s_req->data.value(0).toString();
    To copy to clipboard, switch view to plain text mode 

    I only clear the querry in the begining of the function.

Similar Threads

  1. Load in memory the content of a database
    By franco.amato in forum Qt Programming
    Replies: 24
    Last Post: 3rd January 2011, 17:21
  2. How to insert row to SQLite database?
    By MIH1406 in forum Qt Programming
    Replies: 6
    Last Post: 29th May 2010, 12:22
  3. QT & Sqlite problem: "database is locked"
    By xfurrier in forum Qt Programming
    Replies: 2
    Last Post: 17th July 2009, 07:06
  4. Cannot load SQLITE driver on clean target
    By ForestDweller in forum Installation and Deployment
    Replies: 1
    Last Post: 15th December 2008, 00:49
  5. Load menu from database
    By daica2003 in forum Qt Programming
    Replies: 5
    Last Post: 11th May 2008, 18:18

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.