Results 1 to 17 of 17

Thread: QSqlDatabase retrieve connection in different form

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

    Default QSqlDatabase retrieve connection in different form

    hi,

    according to manual (QSqlDatabase::addDatabase()):
    and subsequent calls to database() without the connection name argument will return the default connection. If a connectionName is provided here, use database(connectionName) to retrieve the connection.
    if i would like to retrieve the database connection I need to use the
    Qt Code:
    1. database()
    To copy to clipboard, switch view to plain text mode 

    i'm not sure if i just need to use
    Qt Code:
    1. database()
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. QSqlDatabase::database()
    To copy to clipboard, switch view to plain text mode 
    - which doesn't exists

    or what else?

    could you please give me an example please? I don't have a name for my connection defined.
    thanks.

  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

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database();
    To copy to clipboard, switch view to plain text mode 
    which does exist in exactly the same place that QSqlDatabase::addDatabase() comes from. Make sure you have "QT += sql" in your PRO file

  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

    which does exist in exactly the same place that QSqlDatabase::addDatabase() comes from. Make sure you have "QT += sql" in your PRO file
    hi,

    I have += sql in my project file.

    I would like to retrieve the database connection in a different file, than where it was initially declared. I will try this code to see if works.

    edit: I tried this
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database();
    2. db.open();
    To copy to clipboard, switch view to plain text mode 

    the app. runs, but the connection is not open. I receive a message in the Application Output.
    QSqlQuery:repare: database not open
    any idea what's not correct? please keep in mind that i declared the db connection in one .cpp file and I would like to retrieve the connection in a different .cpp file.
    cheers.
    Last edited by avpro; 20th August 2013 at 17:30.

  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

    Yes, you have not configured the database connection correctly and it is failing to open.
    QSqlDatabase::open() and QSqlDatabase::lastError() return a value for a reason.

    There are plenty of examples in the reference, overview, and example documentation.

  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

    hi,

    thanks for guidelines, but I'm still struggling to find a solution for my query. maybe you could help.

    my database connection is defined in 1.cpp; everything is fine with the definition is working, tested.
    in 2.cpp, which is a form launched from a button of 1.cpp, I would like to re-open the initial connection and execute a query.

    Qt Code:
    1. QSqlDatabase customer;
    2. customer.open();
    3. QSqlQuery qry1;
    4. qr1.prepare(...);
    5. ...
    6. ...
    7.  
    8. customer.close();
    To copy to clipboard, switch view to plain text mode 


    on the Application output i receive:
    QSqlQuery:repare: database not open
    what's wrong? why i can't open the connection?

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase retrieve connection in different form

    Just show real code where connection is defined.

  7. #7
    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 not initialising customer using QSqlDatabase::database(). See post #2
    You are not checking the open(), prepare() and probably other return values for useful clues. See post #4
    You are not associating qry1 with the database object called customer. See the other QSqlQuery constructor.
    You are executing a different query called qr1 anyway. This is probably the result of retyping/paraphrasing your code rather than copy and pasting it.
    my database connection is defined in 1.cpp; everything is fine with the definition is working, tested.
    For this statement to be true you must have the correct arrangement of code 1.cpp... Why not just adapt that?

    Qt Code:
    1. QSqlDatabase customer = QSqlDatabase::database();
    2. if (customer.open()) {
    3. QSqlQuery qry(customer);
    4. if (qry.prepare(" ... ") && qry.exec()) {
    5. // do stuff with query results
    6. }
    7. else {
    8. // report using QSqlQuery::lastError() information
    9. }
    10. }
    11. else {
    12. // report using QSqlDatabase::lastError() information
    13. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 22nd August 2013 at 21:09. Reason: Fat handed iPad typing

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

    Let’s start again with real code:
    As i already mentioned, my database connection is defined in 1.cpp; everything is fine with the definition is working, tested.

    1.cpp code here:
    Qt Code:
    1. QSqlDatabase customer = QSqlDatabase::addDatabase("QMYSQL");
    2. customer.setHostName("localhost");
    3. customer.setDatabaseName("test");
    4. customer.setUserName("root");
    5. customer.setPassword("pass");
    6. customer.open();
    To copy to clipboard, switch view to plain text mode 


    in 2.cpp, which is a form launched from a button of 1.cpp, I would like to re-open the initial connection and execute a query.
    Qt Code:
    1. QSqlDatabase customer;
    2. customer.open();
    3. QSqlQuery qry1;
    4. qr1.prepare(...);
    5. ...
    6. customer.close();
    To copy to clipboard, switch view to plain text mode 

    In this way, it worked to recall the connection and the query is executed in 2.cpp. I didn’t check and report the lastError yet, but I will integrate it, too.

    After the application is running and if I run the query for two times or more, in the Application Output I receive:

    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.

    Could you please let me know if this is normal, and if not how could I correct it?
    thanks for your input. every little progress makes life easier.

  9. #9
    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, it is not normal or correct. Read my previous post. I show you exactly how to code using the database.

  10. #10
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase retrieve connection in different form

    If you opens db connection in 1.cpp file in 2.cpp file You use it like this :
    Qt Code:
    1. QSqlDatabase customer = QSqlDatabase::database();
    2. QSqlQuery qry1;
    3. qr1.prepare(...);
    4. ...
    5. customer.close();
    To copy to clipboard, switch view to plain text mode 
    Reading the documentation does not hurt. From QSqlDatabase::QSqlDatabase doc : Creates an empty, invalid QSqlDatabase object. Use addDatabase(), removeDatabase(), and database() to get valid QSqlDatabase objects.

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

    thank you all for your input. I will read the referenced documentation and return for an opinion.

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

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

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

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

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

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