PDA

View Full Version : QT - SQLite Connection



sabeesh
3rd August 2007, 10:55
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.....

balazsbela
3rd August 2007, 12:08
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.

jacek
4th August 2007, 01:27
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.

jacek
4th August 2007, 01:29
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.

sabeesh
6th August 2007, 06:39
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() );
}
}

big4mil
6th August 2007, 06:57
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

sabeesh
6th August 2007, 07:10
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....

big4mil
6th August 2007, 07:34
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.

sabeesh
6th August 2007, 07:51
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.

big4mil
6th August 2007, 07:54
and your problem is fixed?

mm78
6th August 2007, 08:03
This is described in the documentation:
http://doc.trolltech.com/4.3/qsqldatabase.html#removeDatabase

big4mil
6th August 2007, 08:13
yeah, but that's not needed in that case. The call of the database close function will do it.

maverick_pol
6th August 2007, 08:45
Hi,

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

Maverick

sabeesh
6th August 2007, 08:52
Hi,
I solve my problum using
setAttribute(Qt::WA_DeleteOnClose);

Thankyou.....

QAmazigh
3rd June 2009, 13:58
this helped me
thanks sabeesh :)