PDA

View Full Version : Using tables from different databases in QSqlRelationalTable



blivrail
10th April 2008, 22:04
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.

wysota
10th April 2008, 22:22
I don't think you can do that. It breaks some normal form anyway, so you should probably redesign your databases.

blivrail
10th April 2008, 23:41
I was hoping it didn't come to that - thanks!

trueneo
11th April 2008, 00:37
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

GreyGeek
14th April 2008, 17:33
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.