Results 1 to 4 of 4

Thread: question about QSqlDatabase and how to make query...

  1. #1
    Join Date
    May 2011
    Posts
    16
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Question question about QSqlDatabase and how to make query...

    Hi everyone ,
    I want to make a database query...
    when I implement a class like:
    Qt Code:
    1. class IntrfcDB
    2. {
    3. public:
    4. IntrfcDB();
    5. ~IntrfcDB();
    6.  
    7. void useDBConn();
    8. }
    9.  
    10. void IntrfcDB::useDBConn()
    11. {
    12. qry.exe("select * ...");
    13. }
    To copy to clipboard, switch view to plain text mode 
    I get the message: database is not open.
    If I rewrite my code like

    Qt Code:
    1. class IntrfcDB
    2. {
    3. public:
    4. IntrfcDB();
    5. ~IntrfcDB();
    6.  
    7. void useDBConn();
    8. }
    9.  
    10. void IntrfcDB::useDBConn()
    11. {
    12. qry.exe("select * ...");
    13. }
    To copy to clipboard, switch view to plain text mode 

    everything works perfectly (I initialize the connection in the constructor which uses the default connection).
    I am new to both c++ and qt. I know that QSqlDatabase is a static function.
    I would like to know is it possible to do such thing using the first method? and how?
    If not, is this because of static function QSqlDatabase::addDatabase?
    Is there anyone to give some explanation Thank you all

  2. #2
    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: question about QSqlDatabase and how to make query...

    The problem here is generic C++ understanding.

    In your first example the qry object is default constructed during creation of the IntrfcDB object before your IntrfcDB constructor code is executed. At this time there is no database open, so the QSqlQueryModel (QSqlQuery) default constructor gets no valid result when it looks for the default db connection.

    In your second example the qry object is not constructed until you call useDBConn() by which time you have set up a default database connection.

    For some members you can give them an initial value in the constructor initialization list (before the constructor code) but this is not generally possibly with objects requiring multiple setup steps like QSqlDatabase. To keep it in the constructor you can allocate the QSqlQueryModel on the heap after creating the connection. You will ultimately need a pointer to the model for most functions that use it anyway.

  3. The following user says thank you to ChrisW67 for this useful post:

    abdol (4th July 2011)

  4. #3
    Join Date
    May 2011
    Posts
    16
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: question about QSqlDatabase and how to make query...

    Thanks a lot ChrisW67 (many times )
    I understood the firs part of your answer but I can't digest the 2nd part of it:
    To keep it in the constructor you can allocate the QSqlQueryModel on the heap after creating the connection. You will ultimately need a pointer to the model for most functions that use it anyway.
    do you mean that I have to create a pointer to QSqlDatabase and then new it in constructor? I will try it to see if I interpreted your answer correctly.
    Thank you again.
    Last edited by abdol; 4th July 2011 at 17:03.

  5. #4
    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: question about QSqlDatabase and how to make query...

    No, you need a pointer to the model for (almost) everything that needs to use a model. So, create the model on the heap (using new) and keep the pointer in the IntrfcDB object rather than an actual instance of QSqlQueryModel. The model is not constructed until you call new, which you can place after the database is set up.
    Qt Code:
    1. class IntrfcDB: public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. IntrfcDB();
    7. ~IntrfcDB();
    8.  
    9. void useDBConn();
    10. }
    11.  
    12. IntrfcDB::IntrfcDB(): qry(0)
    13. {
    14. db = QSqlDatabase::addDatabase(...);
    15. ... other db init
    16.  
    17. qry = new QSqlQueryModel(this);
    18. }
    19.  
    20. void IntrfcDB::useDBConn()
    21. {
    22. qry->exec("select * ...");
    23. }
    To copy to clipboard, switch view to plain text mode 
    I have used QObject to manage deallocation but you could do it manually in the class destructor.

Similar Threads

  1. Replies: 1
    Last Post: 15th December 2010, 13:20
  2. Replies: 6
    Last Post: 18th August 2010, 12:52
  3. Replies: 8
    Last Post: 9th May 2010, 16:43
  4. QSqlRelationalTableModel query question
    By cyberboy in forum Qt Programming
    Replies: 4
    Last Post: 6th February 2008, 18:47
  5. Mysql query question
    By twells55555 in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2007, 23:41

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.