PDA

View Full Version : [SOLVED]: addDatabase with connectionName problem



omci
10th December 2009, 09:49
Hello again!
I have strange problem while dealing with SQLite database. Everything seem so be all right when I open it like this:

QString app_path = QApplication::applicationDirPath();
QString dbase_path = app_path + "/base.bz";
{
QSqlDatabase base = QSqlDatabase::addDatabase("QSQLITE");
base.setDatabaseName(dbase_path);
base.open();
if(base.isOpen() != true){
QMessageBox::critical(this, "Error", "Base is not open.");
}
else {
// the database is opened
// create data table
QSqlQuery sql_create_table("CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY, date TEXT, ...)");
...
}
base.close();
}
Except when I try to reconnect I get alert:

QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
Data is correctly writen and extracted to and from database.
In docs it is writen, that I must use connectionName to avoid this error. I change:

QSqlDatabase base = QSqlDatabase::addDatabase("QSQLITE", "conn_name"); and add
QSqlDatabase::removeDatabase("conn_name"); after I close the database. The alert now say:
QSqlQuery::exec: database not open, nothing is writen to the database. base.isDriverAvailable say, it is not...query.lastError() say Driver not loaded. However base.isOpen() say it is open!
What am I doing wrong? I am using Qt 4.5 that came with Fedora 12, sqlite driver is present (at least it was instaled with fedora and in use without connectionName it is working).
Regards,
Luka

Grimlock
10th December 2009, 11:02
As You have set a name for Your data base connection You will need to specify a connection for the Query to use.
QSqlQuery::QSqlQuery ( const QString & query = QString(), QSqlDatabase db = QSqlDatabase() )



QSqlQuery qry(QSqlDatabase::database("conn_name"));
qry.prepare("....");

omci
10th December 2009, 13:44
That solved my problem. Thank you for fast replay.
Luka

Grimlock
10th December 2009, 13:56
You are welcome. :cool: