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
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows
    Thanked 1 Time in 1 Post

    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.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    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
  •  
Qt is a trademark of The Qt Company.