Results 1 to 4 of 4

Thread: How does QSqlDatabase work?

  1. #1
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 2 Times in 2 Posts

    Default How does QSqlDatabase work?

    Hi,
    there are some issues I can't understand around the database-handling. This is simply some mistery to me...
    So now I connect to a database using a function that was created based on an in-built tutorial.
    Qt Code:
    1. bool createConnection()
    2. {
    3. std::ofstream outf("teszt_connection.txt");
    4. outf << "createConnection\n";
    5.  
    6. QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    7. db.setHostName("myHOst);
    8. db.setPort(1234);
    9. db.setUserName("myUN");
    10. db.setPassword("myPWD");
    11. db.setDatabaseName("myDBN");
    12.  
    13. if (!db.open()) return false;
    14. else return true;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    Now I don't understand how this can estabilish the connection so the databbase can be accessed from anywhere outside? There is a local variable db, and everything is set on this variable. But I can reach the database out of this function too with QSqlQuery objects and there is no need telling it that there has ever been a QSqlDatabase object (namely db) that was set. On the other hand there might questions rise about the database itself and I cannot "ask" them outside this function because those should be functions of a QSqlDatabase class but the variable is no more accessable.

    For example it is impossible to call the function "tables()" in the same scope where createConnection() was called because tables() is for QSqlDatabase instances and the only one, namely db, is available only in the function createConnection(). But how does than the environment know anything then? How can be say more than one databases handled if QSqlQuery does not wait for any database-related identifyer so the command is "sent in the air"? I'm really confused.
    Szilvi

  2. #2
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 11 Times in 9 Posts

    Default Re: How does QSqlDatabase work?

    quote from Qt docs:
    QSqlDatabase also supports the concept of a default connection, which is the unnamed connection. To create the default connection, don't pass the connection name argument when you call addDatabase(). Subsequently, when you call any static member function that takes the connection name argument, if you don't pass the connection name argument, the default connection is assumed.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How does QSqlDatabase work?

    Under the hood there is a static map of connections maintained by Qt. The local variable can go out of scope but its copy is kept in the static map.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    May 2010
    Posts
    86
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    17
    Thanked 2 Times in 2 Posts

    Default Re: How does QSqlDatabase work?

    Szia Szilvi!

    In the examples so far, we have assumed that the application is using a single database connection. If we want to create multiple connections, we can pass a name as a second argument to addDatabase(). For example:

    QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL", "OTHER");
    db.setHostName("saturn.mcmanamy.edu");
    db.setDatabaseName("starsdb");
    db.setUserName("hilbert");
    db.setPassword("ixtapa7");


    We can then retrieve a pointer to the QSqlDatabase object by passing the name to QSqlDatabase::database():

    QSqlDatabase db = QSqlDatabase::database("OTHER");


    To execute queries using the other connection, we pass the QSqlDatabase object to the QSqlQuery constructor:

    QSqlQuery query(db);
    query.exec("SELECT id FROM artist WHERE name = 'Mando Diao'");


    Multiple connections are useful if we want to perform more than one transaction at a time, since each connection can handle only a single active transaction. When we use multiple database connections, we can still have one unnamed connection, and QSqlQuery will use that connection if none is specified.

Similar Threads

  1. what should i include to work with QSqlDatabase?
    By mismael85 in forum Qt Programming
    Replies: 8
    Last Post: 25th December 2011, 18:57
  2. QSQLDatabase
    By hoshy in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2009, 15:09
  3. QSqlDatabase
    By Pragya in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2007, 17:53
  4. QSqlDatabase error
    By maxpower in forum Qt Programming
    Replies: 7
    Last Post: 19th October 2006, 19:00
  5. QSqlDatabase
    By raphaelf in forum Qt Programming
    Replies: 10
    Last Post: 10th May 2006, 15:15

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.