Results 1 to 2 of 2

Thread: How does one use QSqlQuery pointer to connect to a database?

  1. #1
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How does one use QSqlQuery pointer to connect to a database?

    How does one connect a database pointer to a sqlquery pointer. For example,
    Qt Code:
    1. db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE"));
    2. qry = new QSqlQuery(*db);
    To copy to clipboard, switch view to plain text mode 

  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: How does one use QSqlQuery pointer to connect to a database?

    QSqlQuery does not have a constructor taking a QSqlDatabase* so you cannot do what you ask. Also, QSqlDatabase::addDatabase() returns a QSqlDatabase, so there is no need to do anything else to the return value.

    What you want to do, after you have read the docs and looked at the examples (which show this pattern over and over) is:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName("/some/file/path");
    3. // you might need credentials for other database types
    4. if (db.open()) {
    5. QSqlQuery qry(db);
    6. qry.prepare(...);
    7. // etc.
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ChrisW67 for this useful post:

    zim (3rd July 2011)

Similar Threads

  1. [QSqlQuery] Problem with not open database
    By xkazielx in forum Newbie
    Replies: 11
    Last Post: 1st September 2013, 20:32
  2. Connect to database
    By seink in forum Newbie
    Replies: 12
    Last Post: 26th April 2011, 18:43
  3. Replies: 1
    Last Post: 17th January 2010, 07:36
  4. QSqlQuery::exec: database not open
    By newtowindows in forum Qt Programming
    Replies: 8
    Last Post: 29th October 2009, 08:48
  5. Database and QSqlQuery::prepare problem
    By fengtian.we in forum Qt Programming
    Replies: 1
    Last Post: 31st October 2007, 23:17

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.