PDA

View Full Version : Sql, dupicate connection



giusepped
1st November 2008, 06:58
I have two function, one is called at the start time, and the other is called whenever i want to create a new database.




bool mainWindow::createConnection()
{
db = QSqlDatabase::addDatabase("QSQLITE");
/* If the database do not exist
I create one anew*/
QFile check;
check.setFileName(databaseName);
QDir::setCurrent(QCoreApplication::applicationDirP ath());
#ifdef DEBUG
qDebug()<<QCoreApplication::applicationDirPath();
#endif
db.setDatabaseName(databaseName);
if (!check.exists()){
QMessageBox::warning(0,QObject::tr("Database mancante"),"Sembra che il database precedente sia stato cancellato. Ne ho creato uno nuovo con lo stesso nome");
#ifdef DEBUG
qDebug()<<databaseName<<db.open();
#endif
createTable();
}
if (!db.open()) {
QMessageBox::warning(0,QObject::tr("Non riesco a connettermi al database"),"Controlla che un databse sia presente o scegline uno dal menu File");
#ifdef DEBUG
qDebug()<<db.lastError()<<db.open();
#endif
return false;
}
return true;

}



void mainWindow::newDB()
{
/* Creat un nuovo archivio*/
if (db.isOpen())
{
removeConnection();
#ifdef DEBUG
qDebug()<<"newDB, closing conn.:"<<db.isOpen();
#endif
}
QString fileName = QFileDialog::getSaveFileName(this, tr("Nuovo archivio"),QDir::homePath (),tr("Rubic (*.db)"));
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(fileName);
#ifdef DEBUG
qDebug()<<"newDB, creating"<<db.lastError()<<db.isOpen()<<fileName<<db.isValid();
#endif
if (db.isValid()){
createTable();
databaseName=fileName;

}


}


But the debug says to me that, when I call newDB(), removeConnection() and db = QSqlDatabase::addDatabase("QSQLITE");, there is still in use the qt_sql_default connection.
The function removeConnection() is


void mainWindow::removeConnection()
{
// db.close();

QSqlDatabase::removeDatabase(db.connectionName());
}



For this reason, I cannot start a new database.
Help appreciated.

spirit
1st November 2008, 07:41
add connection name as second parameter of QSqlDatabase::addDatabase method.


QSqlDatabase db1 = QSqlDatabase::addDatabase("QSQLITE", "conn1");//creating connection with the first databse
QSqlDatabase db2 = QSqlDatabase::addDatabase("QSQLITE", "conn2");//creating connection withe the second database
....
QSqlDatabase db1 = QSqlDatabase::database("conn1");//getting access to the first database
QSqlDatabase db2 = QSqlDatabase::database("conn2");//getting access to the second database

giusepped
1st November 2008, 14:46
Thank you. But I don't undertand why if I close the connection with close() and then remove qt says "qt_sql_default_connection still active".
In other words, I cannot shut down a connection?
Anyway, I will try tomorrow and let you know my results.

janus
1st November 2008, 16:37
Hi,
maybe there are queries that are still active e.g a queryModel that hasn't fetched all data ...

giusepped
2nd November 2008, 06:01
Hi,
maybe there are queries that are still active e.g a queryModel that hasn't fetched all data ...

The problem is very simple.
1) At the start up I create a connection (default)
2) If the user wants to open another archive I would open a new connection to that database and make it as default.

Now, there are two ways
1) destroy the previous connection and create another one (removeDatabase, or close ...)
2) add the newconnection to the list of connections and make a global variable to track the currentdatabase.

But up to now, all the suggested methods do not work....:confused:

spirit
2nd November 2008, 09:28
2) add the newconnection to the list of connections and make a global variable to track the currentdatabase.

it is not necessary to create global variable for this, just specify connection name as I suggested in my first post and the get access to database by connction name. this method works perfectly.

spirit
2nd November 2008, 09:30
1) At the start up I create a connection (default)
2) If the user wants to open another archive I would open a new connection to that database and make it as default.

it is also not necessary to make connection default, you can pass needed database in ctors of sql classes.

janus
2nd November 2008, 11:36
hi,

i am working with a code like this ...


QString path = QFileDialog::getOpenFileName(this,
tr("Path to DB"), qApp->applicationDirPath(), "SQLite (*.sqlite3)");
if(!path.isEmpty()){
QSqlDatabase::database().close();
QSqlDatabase::database().removeDatabase(QSqlDataba se::database().connectionName());
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(path);
if(!db.open()){
QMessageBox::critical(this, tr("Error"), tr("DB Error"));
}

But I always get the message that the database is still in use. AFAIR you cant get rid of this message with sqlite ... . But the new connection does work.

giusepped
2nd November 2008, 15:34
I just came up with a method which imitates yours.
Every time I check the database connection, and if it is ok I open a connection with the name of the filename. More, I use a member variable to track the current database in action.
In this way, I can always remove the correct database and avoid duplicate connection.