Results 1 to 9 of 9

Thread: Sql, dupicate connection

  1. #1
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Sql, dupicate connection

    I have two function, one is called at the start time, and the other is called whenever i want to create a new database.

    Qt Code:
    1. bool mainWindow::createConnection()
    2. {
    3. db = QSqlDatabase::addDatabase("QSQLITE");
    4. /* If the database do not exist
    5. I create one anew*/
    6. QFile check;
    7. check.setFileName(databaseName);
    8. QDir::setCurrent(QCoreApplication::applicationDirPath());
    9. #ifdef DEBUG
    10. qDebug()<<QCoreApplication::applicationDirPath();
    11. #endif
    12. db.setDatabaseName(databaseName);
    13. if (!check.exists()){
    14. QMessageBox::warning(0,QObject::tr("Database mancante"),"Sembra che il database precedente sia stato cancellato. Ne ho creato uno nuovo con lo stesso nome");
    15. #ifdef DEBUG
    16. qDebug()<<databaseName<<db.open();
    17. #endif
    18. createTable();
    19. }
    20. if (!db.open()) {
    21. QMessageBox::warning(0,QObject::tr("Non riesco a connettermi al database"),"Controlla che un databse sia presente o scegline uno dal menu File");
    22. #ifdef DEBUG
    23. qDebug()<<db.lastError()<<db.open();
    24. #endif
    25. return false;
    26. }
    27. return true;
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void mainWindow::newDB()
    2. {
    3. /* Creat un nuovo archivio*/
    4. if (db.isOpen())
    5. {
    6. removeConnection();
    7. #ifdef DEBUG
    8. qDebug()<<"newDB, closing conn.:"<<db.isOpen();
    9. #endif
    10. }
    11. QString fileName = QFileDialog::getSaveFileName(this, tr("Nuovo archivio"),QDir::homePath (),tr("Rubic (*.db)"));
    12. db = QSqlDatabase::addDatabase("QSQLITE");
    13. db.setDatabaseName(fileName);
    14. #ifdef DEBUG
    15. qDebug()<<"newDB, creating"<<db.lastError()<<db.isOpen()<<fileName<<db.isValid();
    16. #endif
    17. if (db.isValid()){
    18. createTable();
    19. databaseName=fileName;
    20.  
    21. }
    22.  
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. void mainWindow::removeConnection()
    2. {
    3. // db.close();
    4.  
    5. QSqlDatabase::removeDatabase(db.connectionName());
    6. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sql, dupicate connection

    add connection name as second parameter of QSqlDatabase::addDatabase method.
    Qt Code:
    1. QSqlDatabase db1 = QSqlDatabase::addDatabase("QSQLITE", "conn1");//creating connection with the first databse
    2. QSqlDatabase db2 = QSqlDatabase::addDatabase("QSQLITE", "conn2");//creating connection withe the second database
    3. ....
    4. QSqlDatabase db1 = QSqlDatabase::database("conn1");//getting access to the first database
    5. QSqlDatabase db2 = QSqlDatabase::database("conn2");//getting access to the second database
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sql, dupicate connection

    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.

  4. #4
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: Sql, dupicate connection

    Hi,
    maybe there are queries that are still active e.g a queryModel that hasn't fetched all data ...

  5. #5
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sql, dupicate connection

    Quote Originally Posted by janus View Post
    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....

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sql, dupicate connection

    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.

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sql, dupicate connection

    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.

  8. #8
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: Sql, dupicate connection

    hi,

    i am working with a code like this ...

    Qt Code:
    1. QString path = QFileDialog::getOpenFileName(this,
    2. tr("Path to DB"), qApp->applicationDirPath(), "SQLite (*.sqlite3)");
    3. if(!path.isEmpty()){
    4. QSqlDatabase::database().close();
    5. QSqlDatabase::database().removeDatabase(QSqlDatabase::database().connectionName());
    6. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    7. db.setDatabaseName(path);
    8. if(!db.open()){
    9. QMessageBox::critical(this, tr("Error"), tr("DB Error"));
    10. }
    To copy to clipboard, switch view to plain text mode 

    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.

  9. #9
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sql, dupicate connection

    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.

Similar Threads

  1. SQL connection closure problem.
    By cbarmpar in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2008, 08:42
  2. Client/Server Error: BadIDChoice
    By 3nc31 in forum Qt Programming
    Replies: 5
    Last Post: 27th November 2007, 10:22
  3. Connection with MS SQL
    By manish_pesit in forum Qt Programming
    Replies: 4
    Last Post: 13th September 2006, 07:47

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
  •  
Qt is a trademark of The Qt Company.