[SOLVED] Qt 4.7.3, ODBC driver, error when connecting to db
-
wrote on 4 Aug 2011, 10:51 last edited by
I have the following problem with the ODBC driver.
I have 2 versions of QT on the same PC.
- "2010.02.1" used with MSVS2005 (x32)
- "4.7.3" used with MSVS2010 (x64)
I need to connect to DBASE "*.dbf" database. (The odbc driver is compiled as a plugin in both cases)
The connection string is:@ db = QSqlDatabase::addDatabase("QODBC");
QString connect_str("DRIVER={Microsoft dBASE Driver (*.dbf)};DBQ=");
connect_str.append(_db_path);
db.setDatabaseName(connect_str);
if (!db.open()) {
QSqlError err = db.lastError ();
QString s_err("The database reported an error: ");
s_err.append(err.databaseText());
throw std::logic_error(s_err.toAscii().data());
}@In case 1. everything works perfectly
In case 2. I'm getting the error: "Datasource name not found and no default driver specified"I debugged a little, the Qt function which fails is:
qsql_odbc.cpp, line 1885
@r = SQLDriverConnect(...)@Anyone knows the reason?
Eddy : added code tags, please use @ tags for your code
-
wrote on 4 Aug 2011, 16:37 last edited by
Do you have system ODBC drivers for 64bit architecture?
-
wrote on 4 Aug 2011, 17:28 last edited by
What do you mean by system driver?
I have compiled a Qt ODBC driver for 64 bit yes. -
wrote on 4 Aug 2011, 17:37 last edited by
You always need the drivers for your database and some generic system drivers for ODBC. Qt uses those to wrap the calls into its own database abstraction layer. If only one of those is missing, the driver cannot be loaded. So make sure you have 64bit versions of the system ODBC and of the driver for your specific database.
-
wrote on 4 Aug 2011, 17:52 last edited by
Ok. It seems I understood what you mean.
I'm using win 7 x64 for both cases.
I'm sure that I have a correct Qt database driver (Otherwise Qt generates another error smth like "QODBC not loaded"). Now we are coming to system driver. I guess you mean OS driver, something like "odbcad32.exe" right?
My assumption is that this it is loading automatically, since so far I had no problems with that.
But may be you are right, does that means that Windows is loading a 32 bit driver (on 64 bit system) by default and I need 64 bit one? Do you know anything how to switch them? -
wrote on 4 Aug 2011, 18:00 last edited by
As long as your application is 64bit, all loaded libs must be 64bit too. As your application works as 32bit version, it's very likely that you do not have the 64bit drivers installed or in the right path. Fortunately I didn't have to deal with ODBC so far, so unfortunately for you I cannot be of big help here :-)
-
wrote on 4 Aug 2011, 18:05 last edited by
Yes, my all libs all are 64 bit (for case 2), but it seems that the ODBC driver comes with the Office (and it is 32 bits in my case ). It is possible to install "MS AccessDatabaseEngine X64", but then office is not working correctly any more. (according to the forum here (sorry in German) http://www.windows-7-forum.net/windows-7-software/15054-32-bit-64-bit-odbc-problem.html )
But anyway, thanks for pointing me to the right direction, now I know at least where to search :-)
-
wrote on 4 Aug 2011, 18:10 last edited by
You're welcome! If you find a solution, please add to the thread here - some users stepping into that trap later will surely be thankful :-)
-
wrote on 22 Sept 2011, 15:59 last edited by
The solution was really easy:
The connection string must be exactly this@
"DRIVER={Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx)};DBQ=".
@Without those stupid "*.ndx, *.mdx" it doesn't work on x64 but works perfectly on x86.
(Database is *.dbf)
I "love" Microsoft :-)[EDIT: code formatting for the string, Volker]
-
wrote on 13 Jul 2012, 18:57 last edited by
how did you define _db_path? Ive been getting this same problem, along with many others, but I attempted to use this but didnt know what exactly _db_path should be to make it work
-
wrote on 16 Jul 2012, 07:59 last edited by
"_db_path" is just a path to your DB directory, i.e. the location of your DBF files. Note, that you don't need to include a "DBF" filename here, only the path to the directory. The DBF can be accessed then like following:
"SELECT * FROM YOURFILENAME.DBF" -
wrote on 16 Jul 2012, 13:05 last edited by
So I could put the SELECT statement in place of _db_path?
-
wrote on 16 Jul 2012, 13:10 last edited by
No. "_db_path" is appended to the connection string (in the above example).
I mean that you should include only the folder name into the "_db_path".
The filename is used as a table name in the sql query. "SELECT ..." is the SQL query of course. -
wrote on 16 Jul 2012, 13:51 last edited by
Oh Ok. Sorry I'm new to Qt and SQL and. So I would replace _db_path with "C:\Users..." pointing to the folder the .dbf is in? Also can you tell me how to create the .dbf? The only examples I've seen use SQLlight and .dat files so I can't figure out how to make the .dbf or just access a database I made directly in SQL. Would I just add the disired filename here
@QString connect_str("DRIVER={Microsoft dBASE Driver (*.dbf)};DBQ=");@like this
@QString connect_str("DRIVER={Microsoft dBASE Driver (*.dbf)};DBQ=filename");@ -
wrote on 16 Jul 2012, 13:58 last edited by
[quote author="confused" date="1342446698"]So I would replace _db_path with "C:\Users..." pointing to the folder the .dbf is in? [/quote]
Yes
[quote author="confused" date="1342446698"]
Also can you tell me how to create the .dbf?
[/quote]You should execute a query "CREATE TABLE YOURFILE.DBF"