Results 1 to 17 of 17

Thread: QSqlDatabase retrieve connection in different form

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2013
    Location
    Schweiz
    Posts
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QSqlDatabase retrieve connection in different form

    Hi ChrisW67, Hi Lesiok,

    I think I understood the definitions of the classes. I wrote the following code.
    Just for info, I'm running an Win XP 32 bit, MySQL Workbench 5.2.47, Qt 5.0.2


    on 1.cpp:
    Qt Code:
    1. void 1::on_addUser_triggered()
    2. {
    3. QSqlDatabase addUser = QSqlDatabase::addDatabase("QMYSQL", "adConn");
    4. addUser.setHostName("localhost");
    5. addUser.setDatabaseName("test");
    6. addUser.setUserName("root");
    7. addUser.setPassword("pass");
    8. usernew *nU = new usernew;
    9. nU ->show();
    To copy to clipboard, switch view to plain text mode 

    on 2.cpp
    Qt Code:
    1. void 2::on_buttonBox_accepted()
    2. {
    3. QSqlDatabase addUser = QSqlDatabase::database("adConn");
    4.  
    5. if (addUser.open())
    6. {
    7. qDebug () << "opened"; // only for testing
    8. qDebug () << addUser.connectionName(); // only for testing
    9.  
    10. q1.prepare("INSERT INTO test.users (username, pass, name, email)VALUES (:username, :pass, :name, :email)");
    11. q1.bindValue(":username", ui->lnUsername->text());
    12. q1.bindValue(":pass", ui->lnPassword->text());
    13. q1.bindValue(":name", ui->lnName->text());
    14. q1.bindValue(":email", ui->lnEmail->text());
    15. q1.exec();
    16.  
    17. if (q1.exec())
    18. {
    19. qDebug() << "Query executed with success"; // only for testing
    20. }
    21. else
    22. {
    23. qDebug() << "Query Error: " << q1.lastError().text(); // only for testing
    24. qDebug () << addUser.drivers(); // only for testing
    25. }
    26.  
    27.  
    28. }else
    29. {
    30. qDebug() << "Database Error:" << addUser.lastError(); // only for testing
    31. }
    32. addUser.close();
    33. addUser.removeDatabase("adConn");
    34. }
    To copy to clipboard, switch view to plain text mode 


    when I run the app, I receive these messeges:

    opened - this means the connection is opened.
    "adConn" - the connection name is confirmed
    QSqlQuery:repare: database not open - Why I get this error?
    Query Error: "Driver not loaded Driver not loaded" - Why I get this error?
    ("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3") - due to the above error I checked the drivers. MySql drivers are loaded. I run the tutorial basicQuery from here and the database is opened and I can insert new values
    QSqlDatabasePrivate::removeDatabase: connection 'adUserConn' is still in use, all queries will cease to work. - Why I get this error? is the ::removeDatabase declaration not used correctly?

    I would appreciate if I could receive some guidelines on the below questions:
    1. QSqlQuery:repare: database not open - Why I get this error?
    2. Query Error: "Driver not loaded Driver not loaded" - Why I get this error?
    3. QSqlDatabasePrivate::removeDatabase: connection 'adUserConn' is still in use, all queries will cease to work. - Why I get this error?

    thanks for your support.

  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: QSqlDatabase retrieve connection in different form

    You are receiving the first two error messages because there is no default database connection.
    First listing line 3: you are creating a named connection to a MySql database.
    Second listing line 10: you are creating a QSqlQuery object associated with the default connection not the named connection you created earlier. Take a closer look at the QSqlQuery constructor docs.

    In general you call addDatabase() only once per-connection per-program-run and use QSqlDatabase::database() to access existing connections when needed. If you call it multiple times, in your case every time a button is pushed, you get your last warning message because you are trashing a connection that other objects may still be related to.

  3. #3
    Join Date
    May 2013
    Location
    Schweiz
    Posts
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QSqlDatabase retrieve connection in different form

    you are right, the query should be used with the database connection. I spent a few hours to find in http://qt-project.org/doc/qt-4.8/qsqlquery.html where this was mentioned.

    maybe to help other in the future in line 10 of 2.cpp the query should be called using the database connection name:
    Qt Code:
    1. QSqlQuery q1(yourDatabase.database("yourDatabaseConnectionName"));
    To copy to clipboard, switch view to plain text mode 

    one thing remained unsolved:
    QSqlDatabasePrivate::removeDatabase: connection 'adUserConn' is still in use, all queries will cease to work. - How could I close the database connection to get rid of this error and to avoid any leaks?

    i checked the documentation here, but I still don't understand.

    if i write the
    Qt Code:
    1. addUser.removeDatabase("adConn");
    To copy to clipboard, switch view to plain text mode 
    ouside of my void newUser, I will get other errors. it should be outside of the "if", but the error is still there. any input for this?

    you are trashing a connection that other objects may still be related to.
    is this "trashing" not the correct way to do it?
    how to close it and bin it?

    thanks.

  4. #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

    Default Re: QSqlDatabase retrieve connection in different form

    No, you are still missing the point. The QSqlQuery at line 10 should be created thus:
    Qt Code:
    1. QSqlQuery q1(addConn);
    To copy to clipboard, switch view to plain text mode 
    using the database reference you retrieved at line three. Or, you can create the database connection (i.e addDatabase()) in the first place without a name making it the default connection and all subsequent QSqlQuery objects will associate with that by default.

    When you call addDatabase() multiple times you are effectively closing the existing connection, removing its definition (i.e. QDatabase::removeDatabase()) from underneath any object that may still hold a reference to it (i.e. any existing QSqlQuery associated with the connection), and recreating a potential completely unrelated database connection in its place. That is what the warning is about. This is not the same thing as QDatabase:pen()/QDatabase::close() on a connection that is already defined, you can do this as much as makes sense. It is unusual to completely redefine a database connection after its initial setup. Generally a program will contain a single addDatabase() call during startup and no removeDatabase() calls, leaving clean up for program exit.

  5. #5
    Join Date
    May 2013
    Location
    Schweiz
    Posts
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QSqlDatabase retrieve connection in different form

    I'm not sure if I understand.

    using the database reference you retrieved at line three.
    that's what I'm trying to do, just to use the recall of database connection.

    Or, you can create the database connection (i.e addDatabase()) in the first place without a name making it the default connection and all subsequent QSqlQuery objects will associate with that by default.
    I'm aware about this option. i think using a connection name is safer. it leaves no space for potential error.

    When you call addDatabase() multiple times you ...
    I'm not calling addDatabase multiple times. Is just once in 1.cpp line 3.

    No, you are still missing the point. The QSqlQuery at line 10 should be created thus:
    Qt Code:
    QSqlQuery q1(addConn);
    I tried and is not recognizing the addConn. error: adConn was not declared in this scope.

    i tried
    Qt Code:
    1. QSqlQuery q1("addConn")
    To copy to clipboard, switch view to plain text mode 
    and gives me the below error:
    QSqlQuery::exec: database not open
    QSqlQuery:repare: database not open
    Query Error: "Driver not loaded Driver not loaded"
    QSqlDatabasePrivate::removeDatabase: connection 'adUserConn' is still in use, all queries will cease to work.
    the only solution working is the one described in post 14.

    i will remove the
    Qt Code:
    1. QSqlDatabase::removeDatabase("addConn");
    To copy to clipboard, switch view to plain text mode 
    , close the database connection with
    Qt Code:
    1. addUser.database("addConn").close();
    To copy to clipboard, switch view to plain text mode 

    and
    leaving clean up for program exit.
    I wait and see if other errors strike against the current one. thanks for your support.

  6. #6
    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: QSqlDatabase retrieve connection in different form

    I'm not calling addDatabase multiple times. Is just once in 1.cpp line 3.
    Yes you are, once every time on_addUser_triggered() is called.
    I tried and is not recognizing the addConn. error: adConn was not declared in this scope.
    Sorry, my bad. I meant addUser, the reference you retrieve at line 3 of the second listing.
    i tried
    Qt Code:
    1. QSqlQuery q1("addConn")
    To copy to clipboard, switch view to plain text mode 
    and gives me the below error:
    The QSqlQuery constructor that takes a QString argument is expecting an SQL query and optionally a reference to a database connection, not the name of a database connection. It is interpreting "adConn" as a query on the default database connection. There is no default database connection, hence the error.

    i will remove the
    Qt Code:
    1. QSqlDatabase::removeDatabase("addConn");
    To copy to clipboard, switch view to plain text mode 
    ,
    Good this will remove some of the run time warnings.
    close the database connection with
    Qt Code:
    1. addUser.database("addConn").close();
    To copy to clipboard, switch view to plain text mode 
    You can close the connection to the database with:
    Qt Code:
    1. addUser.close();
    To copy to clipboard, switch view to plain text mode 
    Just as you did in Listing 2. This does not remove the connection definition known by the name "addConn", which remains available for use. The connection will be reopened by default the next time you do something like this:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database("addConn");
    To copy to clipboard, switch view to plain text mode 
    anywhere in your program. You can suppress the automatic open() but that is not broadly useful.

    In short:
    • addDatabase() defines the existence of a database of a certain type to the program and loads a plugin to suit.
    • removeDatabase() completely closes and forgets a previously defined database.
    • database() retrieves a reference to a database connection so you can use it
    • open() opens the connection to a defined database
    • close() close the connection to a defined database.

Similar Threads

  1. How to set x509 on a QSqlDatabase Connection?
    By m3rlin in forum Qt Programming
    Replies: 24
    Last Post: 21st February 2012, 04:04
  2. Windows OCI QSqlDatabase connection
    By hollyberry in forum Newbie
    Replies: 10
    Last Post: 13th February 2012, 22:13
  3. QSqlDatabase Connection Close on Destruction
    By Sanuden in forum Qt Programming
    Replies: 1
    Last Post: 1st September 2011, 15:32
  4. QSqlDatabase connection timeout?
    By joseprl89 in forum Qt Programming
    Replies: 6
    Last Post: 27th March 2011, 01:43
  5. Replies: 3
    Last Post: 22nd June 2006, 16:27

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.