Results 1 to 15 of 15

Thread: QT - SQLite Connection

  1. #1
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Red face QT - SQLite Connection

    Hi,
    I create a program which one connect to a SQLite database, and the program is working. I can read data from database. But the problum is that, when I exit from the program an error message is print on the consol. The error is like this, " QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work. ". How can I solve this problem.

    I create the connection using the following command
    QSqlDatabase db;
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("ManagerDB.db");


    please help me.....

  2. #2
    Join Date
    Jul 2007
    Location
    Cluj Napoca, Romania
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    Hello, I'm sorry I can't help you with your problem, but I just wanted to ask you if you can provide a link to your source code because I am planning on using SQLite in one of my projects and it would be very useful to have some code as reference.
    Only if you are not against the idea.

    Thanks.
    May the source be with you!

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    Quote Originally Posted by sabeesh View Post
    But the problum is that, when I exit from the program an error message is print on the consol. The error is like this, " QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work. ". How can I solve this problem.
    There must be some query or model object still in the memory. Make sure you delete all QtSql objects before you destroy QApplication. Valgrind might help you to track them down.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    Quote Originally Posted by balazsbela View Post
    I am planning on using SQLite in one of my projects and it would be very useful to have some code as reference.
    Take a look at examples in examples/sql/ directory. They all use SQLite.

  5. #5
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    Hi,
    This is the code for create a conection between QT and SQLite and read data from table.


    class Manager : public QMainWindow, private Ui::MainWindow
    {
    Q_OBJECT
    QSqlDatabase db;
    QSqlQuery query;

    public:
    Manager();
    ~Manager();
    bool createConnection();
    void LoadData();

    public slots:
    void play1();
    void stop1();
    void pause1();
    protected:
    };


    bool Manager::createConnection()
    {
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("ManagerDB.db");
    if (!db.open()) {
    QMessageBox::critical(0, qApp->tr("Cannot open database"),
    qApp->tr("Unable to establish a database connection.\n"
    "This program needs SQLite support. Please read "
    "the Qt SQL driver documentation for information how "
    "to build it.\n\n"
    "Click Cancel to exit."), QMessageBox::Cancel);
    return false;
    }
    return true;
    }


    void Manager::LoadData()
    {
    QSqlQuery query1 ("select * from TreeList" );
    while (query1.next()) {
    QString country = query1.value(2).toString();
    qDebug( query1.value(2).toString() );
    }
    }

  6. #6
    Join Date
    Nov 2006
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    Hi,

    where do you close your database connection? You have to use the QSqlDatabase close() function. In your case: db.close(). Put this in the destructor of your "Manager" class and the problem should be solved.

    regds
    big4mil

  7. #7
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    Hi,
    I give it on destructor. But the problem is that, the destructor is not working. How can i do that. I want to call the destructor when I close the Mainwindow. How can I do that?
    Please help me....

  8. #8
    Join Date
    Nov 2006
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    your destructor is called automatically when your mainwindow object is destroyed. How and where do you create your mainwindow object and how does your destructor looks like? Please paste your code, so we can help you.

  9. #9
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    Hi,
    Thankyou for your help.
    I solve the problum by setting this attribute on the constructor of main window.

    setAttribute(Qt::WA_DeleteOnClose);

    After setting this, the destructor is working when I close the main window.

  10. #10
    Join Date
    Nov 2006
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    and your problem is fixed?

  11. #11
    Join Date
    Oct 2006
    Posts
    42
    Thanks
    1
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT - SQLite Connection

    This is described in the documentation:
    http://doc.trolltech.com/4.3/qsqldat...removeDatabase

  12. #12
    Join Date
    Nov 2006
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    yeah, but that's not needed in that case. The call of the database close function will do it.

  13. #13
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QT - SQLite Connection

    Hi,

    I belive you may create a multiple connection to your database.
    Paste your code here so we can help you.

    Maverick
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  14. #14
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    Hi,
    I solve my problum using
    setAttribute(Qt::WA_DeleteOnClose);

    Thankyou.....

  15. #15
    Join Date
    May 2009
    Posts
    28
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QT - SQLite Connection

    this helped me
    thanks sabeesh

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. Replies: 3
    Last Post: 2nd August 2007, 21:28
  3. How do I keep the client connection open ?
    By probine in forum Newbie
    Replies: 2
    Last Post: 25th March 2006, 19:06
  4. Can I launch a dial-up connection in Windows?
    By gtthang in forum Qt Programming
    Replies: 3
    Last Post: 9th February 2006, 12:32

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.