PDA

View Full Version : two databases opened at the same time



whoops.slo
26th January 2006, 18:57
Hi!

Me again with my database based problems.
I have opened two databases in order to transfer data from one database to another. I declare first query as:


QSqlQuery query_read("SELECT * FROM groups", db1);
int is_kat = query_read.record().indexOf("group");
while(query_read.next()) {
QString s_kat = query_read.value(is_kat).toString();
//some code + declaration for another query
}

The second query is declared inside the marked area like this:


QSqlQuery query_write(db2);
query_write.prepare("INSERT INTO kat (kategories) VALUES (:kategorie)");
query_write.bindValue(":kategorija", s_kat);
query_write.exec();

Compiling does not return any error, but the data is not transfered. Where is the error?

Regards,
Luka

wysota
26th January 2006, 23:37
":kategorie" and ":kategorija" -- they should be the same.

whoops.slo
27th January 2006, 08:23
tnx... :o I'm ashamed... :o i stared for two hours at the screen but i never see that error... now it works fine!

tnx again!
Luka

whoops.slo
28th January 2006, 09:46
I try to open two seperate SQLite databases at the same time using code below. Why do I get message that the first DB is not opened?


QSqlDatabase base1 = QSqlDatabase::addDatabase("QSQLITE");
QString pot = ui.p_odpri->text();
base1.setDatabaseName(pot);
base1.open();
QSqlDatabase base2 = QSqlDatabase::addDatabase("QSQLITE");
base2.setDatabaseName(qApp->applicationDirPath() + "/base.db");
base2.open();
if (base1.isOpen() != TRUE) {
QMessageBox::critical(this, "Dnevnik", "Error opening DB 1");
}
else {
if(base2.isOpen() != TRUE){
QMessageBox::critical(this, "Dnevnik", "Error opening DB 2");
}
else {
//some code to execute
}
}
base2.close();
base1.close();

If I use code below everything works just fine...


QSqlDatabase base1 = QSqlDatabase::addDatabase("QSQLITE");
QString pot = ui.p_odpri->text();
base1.setDatabaseName(pot);
base1.open();
if (base1.isOpen() != TRUE) {
QMessageBox::critical(this, "Dnevnik", "Error opening DB 1");
}
else {
QSqlDatabase base2 = QSqlDatabase::addDatabase("QSQLITE");
base2.setDatabaseName(qApp->applicationDirPath() + "/base.db");
base2.open();
if(base2.isOpen() != TRUE){
QMessageBox::critical(this, "Dnevnik", "Error opening DB 2");
}
else {
//some code to execute
}
base2.close();
}
base1.close();

Can somebody explain what I do with the first and what with the second code?

Regards,
Luka

zlatko
28th January 2006, 15:35
try use QSqlDatabase::lastError, for finding solution

jacek
28th January 2006, 15:38
I try to open two seperate SQLite databases at the same time using code below. Why do I get message that the first DB is not opened?


From Qt docs:
QSqlDatabase QSqlDatabase::addDatabase ( const QString & type, const QString & connectionName = QLatin1String( defaultConnection ) ) [static]
Adds a database to the list of database connections using the driver type and the connection name connectionName. If there already exists a database connection called connectionName, that connection is removed.
You open the same connection twice.

Try:
QSqlDatabase base1 = QSqlDatabase::addDatabase( "QSQLITE" );
// ...
QSqlDatabase base2 = QSqlDatabase::addDatabase( "QSQLITE", "second_or_whatever" );