
Originally Posted by
Lykurg
Declare them always on need in place.
You likely put a wrong database to query.
You are right. I declared it in place where I needed it and gived as an argument my database and it works. Now I'm just curious about something. I've wrote something like this, just to see what will happen (function that should read one thing from database and then close it):
bool MainWindow::readFromDB()
{
bool ok;
bdb.setDatabaseName(databaseName);
ok = bdb.open();
if (ok)
{
queryModel.clear();
queryModel.setQuery("SELECT * FROM table WHERE name='var1'", bdb);
rec = queryModel.record(0);
notificationInterval = (rec.value(1).toInt())*1000;
rec.clear();
queryModel.clear();
bdb.close();
} else {
out << "Nie udalo sie otworzyc bazy!" << endl;
}
return ok;
}
bool MainWindow::readFromDB()
{
bool ok;
QTextStream out(stdout);
QSqlRecord rec;
QSqlQueryModel queryModel;
bdb = QSqlDatabase::addDatabase("QSQLITE","vb");
bdb.setDatabaseName(databaseName);
ok = bdb.open();
if (ok)
{
queryModel.clear();
queryModel.setQuery("SELECT * FROM table WHERE name='var1'", bdb);
rec = queryModel.record(0);
notificationInterval = (rec.value(1).toInt())*1000;
rec.clear();
queryModel.clear();
bdb.close();
} else {
out << "Nie udalo sie otworzyc bazy!" << endl;
}
QSqlDatabase::removeDatabase("vb");
return ok;
}
To copy to clipboard, switch view to plain text mode
And it does what it should but gives in console:
QSqlDatabasePrivate::removeDatabase: connection 'vb' is still in use, all queries will cease to work.
QSqlDatabasePrivate::removeDatabase: connection 'vb' is still in use, all queries will cease to work.
To copy to clipboard, switch view to plain text mode
Is is normal behavior?
thanks in advance
best regards
Tomasz
Bookmarks