PDA

View Full Version : SQLITE ATTACH database



drescherjm
31st October 2009, 22:43
Is it possible to attach an SQLITE database and still use the Qt database classes?

Lykurg
1st November 2009, 07:21
Well, you could use the sql statement ATTACH DATABASE filename AS databasename then it is pure SQL and has nothing to do with Qt. => You can still use Qt's classes.

Or you can simply use
QSqlDatabase::addDatabase("path/to/file", "mySecondDatabase");
//...
QSqlQuery q(QSqlDatabase::database("mySecondDatabase"));

drescherjm
1st November 2009, 08:44
Well, you could use the sql statement ATTACH DATABASE filename AS databasename then it is pure SQL and has nothing to do with Qt. => You can still use Qt's classes.

Thanks. I was not sure if that method would work.



Or you can simply use
QSqlDatabase::addDatabase("path/to/file", "mySecondDatabase");
//...
QSqlQuery q(QSqlDatabase::database("mySecondDatabase"));


Now that I expected to work.


Now the question is mainly can I use 2 databases in the same QSqlQuery? I assume with the ATTACH database sql statement I can do this.

tavioman
8th December 2009, 22:21
Hello all!

Regarding the same subject.

It is possible to attach a database that resides into a QTemporaryFile?

Thank you!

Lykurg
9th December 2009, 00:09
It is possible to attach a database that resides into a QTemporaryFile?

Have you tried? Why shouldn't it working? It's a "normal" file. Just use QTemporaryFile::fileName() to work with the file.

spirit
9th December 2009, 06:17
Now the question is mainly can I use 2 databases in the same QSqlQuery? I assume with the ATTACH database sql statement I can do this.

with Qt classes you need add two databases using QSqlDatabase::addDatabase, but you need specify different connectionName:


QSqlDatabase db1 = QSqlDatabase::addDatabase("QSQLITE", "db1");
db1.setDatabaseName("path/to/db1");
QSqlDatabase db2 = QSqlDatabase::addDatabase("QSQLITE", "db2");
db2.setDatabaseName("path/to/db2");
//opening databases and etc.


then you can get data from these databases like this:


...
QSqlDatabase db1 = QSqlDatabase::database("db1");
QSqlDatabase db2 = QSqlDatabase::database("db2");

QSqlQuery query1(db1);
//preparing query and execution
...
QSqlQuery query2(db2);
//preparing query and execution
...

spirit
9th December 2009, 06:21
Or you can simply use
QSqlDatabase::addDatabase("path/to/file", "mySecondDatabase");
//...
QSqlQuery q(QSqlDatabase::database("mySecondDatabase"));

are you sure that your code will work?
the first parameter of QSqlDatabase::addDatabase should be a name of a driver? i.e. should be:


QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "mySecondDatabase");
db.setDatabaseName("path/to/file");

//...
QSqlQuery q(QSqlDatabase::database("mySecondDatabase"));

Lykurg
9th December 2009, 07:09
are you sure that your code will work?

Of course not, it was a typo, but since drescherjm figured it out himself...

And I just wonder, why people often ask questions like "Is fooBar working?" without trying it themself before asking. Then they would realize, that it is working.

spirit
9th December 2009, 07:25
And I just wonder, why people often ask questions like "Is fooBar working?" without trying it themself before asking. Then they would realize, that it is working.

agree w you. :)