Results 1 to 2 of 2

Thread: A database question

  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default A database question

    I have a class, and now I wish to add database connections to the constructor for that class.
    I have code like

    Qt Code:
    1. for (int i=0;i<nr_of_connects;i++)
    2. {
    3. QString name("mydb_con");
    4. name.append(QString(i));
    5. X = QSqlDatabase::addDatabase("QMYSQL",name);
    6. X.open();
    7. }
    To copy to clipboard, switch view to plain text mode 
    The problem is the "X" part. What do I write there? Should I declare a QList<QSqlDatabase> in the class or QList<QSqlDatabase*> ?

    I will not know how many database connections there will be at compile time :-(

  2. #2
    Join Date
    Oct 2006
    Posts
    42
    Thanks
    1
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A database question

    addDatabase() returns a QSqlDatabase object and nothing but a QSqlDatabase object so X has to be of type QSqlDatabase.

    Anyhow, QSqlDatabase will handle all the connections for you behind the scenes so you don't have to put them in a list - or keep them around at all. Just use QSqlDatabase::database() whenever you need one of the connections that you have created.

    Relevant docs:
    http://doc.trolltech.com/4.4/qsqldat...ml#addDatabase
    http://doc.trolltech.com/4.4/qsqldatabase.html#database

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. cannot share the database connection!!!!
    By cbarmpar in forum Qt Programming
    Replies: 13
    Last Post: 23rd September 2008, 14:42
  3. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  4. Database Master-Detail Entry Form
    By Phan Sin Tian in forum Newbie
    Replies: 4
    Last Post: 3rd February 2008, 14:31
  5. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 17:53

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.