PDA

View Full Version : QSqlDatabase::open() fails with QODBC



grzywacz
22nd August 2006, 12:10
[solved]

Hi everyone,

I've searched the forum, but none of the previous odbc-related threads has answered my question... :( I can't open an ODBC data source, my program (sample source below) fails with an obscure error message which reads:

"Driver not loaded Driver not loaded"

Yes, it's repeated twice. I try to connect to a database (either MySQL or PostgreSQL) through unix odbc. I have the drivers installed and configured, as well as samle DSNs, and I can browse throug them with DataManager - everything works fine. I've compiled the QSqlODBC plugin from sources (the same version as the library I have installed from Debian's packages) and put it in /usr/lib/qt4/pluings/sqldrivers. The QSqlDatabase::drivers() returns:

("QPSQL7", "QPSQL", "QMYSQL3", "QMYSQL", "QSQLITE", "QSQLITE2", "QODBC3", "QODBC")

Therefore I belive that the plugin is ok. Just to make sure, I've tried to compile it with ODBC_CHECK_DRIVER undefined, as suggested in the documentation - no luck.

My code looks like:



QSqlDatabase db;
db.addDatabase("QODBC");
db.setDatabaseName("Driver={MySQL};Server=localhost;Database=test;Uid= test;Pwd=test;");
if(!db.open()) {
qDebug() << db.lastError().text();
exit(1);
}

Any clues? :confused:

e8johan
22nd August 2006, 12:54
I suppose that the OBDC driver depends on some other lib/plug-in at a lower level. Is that dependency in place?

grzywacz
22nd August 2006, 13:17
I'm not aware of any extra dependencies. The unix odbc libraries are installed in /usr/lib, so the dynamic linker shouldn't have any problems finding them. In turn, the unix odbc seems to be properly configured, as the admin tools work ok. I've tried using strace, but nothing meaningful has shown up.

grzywacz
22nd August 2006, 17:51
Ok, I've just solved it - I've made a mistake in another place, sorry for the useless thread.

ajg85
7th April 2010, 03:27
Yes, it's repeated twice.
This is because the text() method of QSqlError instance concatenates the driver and database error text most likely.



QSqlDatabase db;
db.addDatabase("QODBC"); // addDatabase returns an initialized instance (loads driver)
db.setDatabaseName("Driver={MySQL};Server=localhost;Database=test;Uid= test;Pwd=test;"); // for the database name you'll want to pass the DSN name when using ODBC
if(!db.open()) {
qDebug() << db.lastError().text();
exit(1);
}

Try something like this:



QSqlDatabase db(QSqlDatabase::addDatabase("QODBC"));
db.setDatabaseName("test"); // be sure to define a DSN with whatever name you pass here
if (!db.open())
qDebug() << db.lastError().text();


cheers