Results 1 to 3 of 3

Thread: QslDatabase and default connection

  1. #1
    Join Date
    Aug 2006
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Question QslDatabase and default connection

    Hi
    I m working on a wrapper class for a data base connection.
    I have a member of class QSqlDatabase which is initialzed using the following call:

    Qt Code:
    1. QSqlDatabase::addDatabase(dbDriverName,"QDB")
    To copy to clipboard, switch view to plain text mode 

    I connect to the data base and open the connection as following

    Qt Code:
    1. db.setHostName("localhost");
    2. db.setDatabaseName("dbname");
    3. db.setUserName("dbuser");
    4. db.setPassword("dbpassword");
    5. db.open();
    To copy to clipboard, switch view to plain text mode 
    It's all fine. As you can see the connection uses a name "QDB".

    I use QslQuery to do what I have to do with the data base and it fails because the connection is not opened (message from db.lastError())

    At the end once I'm done, the data base connection is closed and the data base is removed using
    Qt Code:
    1. db.close();
    2. QSqlDatabase::removeDatabase("QDB");
    To copy to clipboard, switch view to plain text mode 

    While trying to query the data base QsqlQuery::exec() fails because the connection is not opened.
    The call to removeDataBase complains that the connection "QDB" is still use while supposedly it failed just before (for default connection ?!)


    If I do the exact same things but instead using a named connection I used the default connection QsqlQuery::exec() proceeds normally but at the end while trying to remove the database I have a warning concerning the default connection "still in use".

    I put a break point in Qt src code and to my surprise QslQuery increments a ref counter of QSqlDatabase() instances. If this counter is != of 1 while calling removeDatabase() for the default connection it complains ("still in use" warning).

    For a slightly more simple code example every thing works fine but the final removeDatabase complaint ("still in use").
    Here is the code I used:

    Qt Code:
    1. QSqlDatabase db= QSqlDatabase::addDatabase("QMYSQL");
    2. db.setHostName("localhost");
    3. db.setDatabaseName("dbname");
    4. db.setUserName("dbuser");
    5. db.setPassword("dbpasword");
    6. db.open();
    7.  
    8. QSqlQuery query;
    9. QString s("DROP TABLE IF EXISTS TOTO");
    10. if(query.exec(s))
    11. {
    12. query.finish();
    13. s.clear();
    14. s.append("CREATE TABLE toto ");
    15. s.append("(id int(8) NOT NULL PRIMARY KEY,");
    16. s.append("name character varying(100));");
    17. if(!query.exec(s))
    18. cout<<"error for query :"<<s.toStdString()<<endl;
    19. }
    20. else
    21. cout<<"error for query :"<<s.toStdString()<<endl;
    22.  
    23. if(db.isOpen())db.close();
    24. db.QSqlDatabase::removeDatabase(db.connectionName());
    To copy to clipboard, switch view to plain text mode 


    So I come to my question.
    What is the best way to use QslDatabase and QslQuery without getting this annoying warning message.

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QslDatabase and default connection

    The best way is to pass the database descriptor to QSqlQuery's constructor.

    Qt Code:
    1. QSqlQuery query(db);
    To copy to clipboard, switch view to plain text mode 
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Nov 2007
    Posts
    17
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QslDatabase and default connection

    Unfortunately QSqlDatabase is unable to verify his state, so it's unnecessary to call close() or remove(): Look this: Qt-Bug224

Tags for this Thread

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.