Results 1 to 5 of 5

Thread: Using tables from different databases in QSqlRelationalTable

  1. #1
    Join Date
    Apr 2008
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Using tables from different databases in QSqlRelationalTable

    Hello,

    It's my first time posting here - hello

    I am having some trouble with using QSqlRelationalTable, setting relation rules using QSqlRelation. I have a case where the relation is defined in a table in another database (with the same connection). I have tried using the syntax

    model->setRelation( col, QSqlRelation( "dbname.tablename", "key", "value" ) ;

    but it doesn't work. I've also tried using ticks (`) in the names as well...

    Does anyone know if there is a workaround possible for this feature?

    Thanks.

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

    Default Re: Using tables from different databases in QSqlRelationalTable

    I don't think you can do that. It breaks some normal form anyway, so you should probably redesign your databases.

  3. #3
    Join Date
    Apr 2008
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using tables from different databases in QSqlRelationalTable

    I was hoping it didn't come to that - thanks!

  4. #4
    Join Date
    Aug 2007
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using tables from different databases in QSqlRelationalTable

    QSqlRelationalDelegate internally uses QSqlTableModel to supply the "related data", QSqlRelationalTableModel fires
    QSqlTableModel::setTable( QSqlRelation::tablename);
    QSqlTableModel::select();
    when the delegate ask for the related data.
    If you could use a table stored in a different database within the same connection, as data source for QSqlTableModel, then you will be able to succeed. I'm sure it is quite difficult, I'm with wysota, redesign your database: first time I see a foreign key in a foreign table in a foreign database.

    Daniele

  5. #5
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using tables from different databases in QSqlRelationalTable

    Quote Originally Posted by blivrail View Post
    Hello,

    It's my first time posting here - hello

    I am having some trouble with using QSqlRelationalTable, setting relation rules using QSqlRelation. I have a case where the relation is defined in a table in another database (with the same connection). I have tried using the syntax

    model->setRelation( col, QSqlRelation( "dbname.tablename", "key", "value" ) ;

    but it doesn't work. I've also tried using ticks (`) in the names as well...

    Does anyone know if there is a workaround possible for this feature?

    Thanks.
    http://doc.trolltech.com/4.0/qsqldatabase.html
    Detailed Description

    The QSqlDatabase class represents a connection to a database.

    The QSqlDatabase class provides an abstract interface for accessing database backends. It relies on database-specific QSqlDrivers to actually access and manipulate data.

    The following code shows how to initialize a connection:

    QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    db.setHostName("acidalia");
    db.setDatabaseName("customdb");
    db.setUserName("mojito");
    db.setPassword("J0a1m8");
    bool ok = db.open();

    Once a QSqlDatabase object has been created you can set the connection parameters with setDatabaseName(), setUserName(), setPassword(), setHostName(), setPort(), and setConnectOptions(). Once the parameters have been set up you can call open() to open the connection.

    The connection defined above is a nameless connection. If is the default connection and can be accessed using database() later on:

    QSqlDatabase db = QSqlDatabase::database();

    To make programming more convenient, QSqlDatabase is a value class. Any changes done to a database connection through one QSqlDatabase object will affect other QSqlDatabase objects representing the same connection. Call cloneConnection() if you want to create an independent database connection based on an existing one.

    If you need multiple database connections simultaneously, specify an arbitrary name to addDatabase() and database(). Call removeDatabase() to remove connections.


    QSqlDatabase will output a warning if you try to remove a connection referenced by other QSqlDatabase objects. Use contains() to see if a given connection name is in the list of connections.

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.