View Full Version : Connecting to an ODBC Data Source
kikapu
22nd June 2007, 12:14
Hi to all folks here.
i am trying to open a QSqlDatabase connection to an Sql Server of mine. I have created an ODBC data source callded "herc" in the Windows and wrote this code:
from PyQt4 import QtSql
db = QtSql.QSqlDatabase.addDatabase("QODBC");
db.setDatabaseName("herc");
db.setUserName("sa");
db.setPassword("the paswsword");
ok = db.open();
but i receive: "QSqlDatabase: QODBC3 driver not loaded
QSqlDatabase: available drivers: "
I am using Qt4.2 Open Source edition, i read the docs and i think that the above is correct. Do i have to do something else too to have this thing work ??
Thanks a lot for any help!
kikapu
23rd June 2007, 00:07
Anyone ??
Is it something to do with the OpenSource version of Qt that i use ??
kikapu
23rd June 2007, 12:23
I think, not much people are doing DB stuff here...:)
Anyway, i found something that a plugn must be create frst so the whole thing could work and i did this (from trolltech):
cd %QTDIR%\src\plugins\sqldrivers\odbc
qmake -o Makefile odbc.pro
nmake
so it did write a file to c:\Qt\4.2.3\src\plugins\sqldrivers\libqsqlodbc.a
but how can i use this ?? The page talks about "loading" the plugin but how i can atually load this and have db code to work ?
Client libraries for Sql Server are available on my machine, so now what ? :confused:
EDIT: i also see in the same directory that a qsqlodbc.dll is created.I tried to copy that dll into the bin directory of QT but the result is the same.
I somehow miss the how i can "load" this plugin...
jacek
23rd June 2007, 14:29
Can you see QODBC driver when you start the sqlbrowser demo?
kikapu
23rd June 2007, 14:38
Hey jacek,
thanks for the reply!
Yes, i see the QODBC driver and in fact, i can fetch tables in the SqlBrowser demo (!), based on the odbc connection i have already declared in windows (i called it "pm").
The code that i wrote is still give me the same error:
QSqlDatabase: QODBC driver not loaded
QSqlDatabase: available drivers:
Have you any idea about this ??
The code is here (notice that i have already tried with both Connection String and ODBC dns ways...)
from PyQt4 import QtCore, QtSql
db = QtSql.QSqlDatabase.addDatabase("QODBC");
db.setHostName("localhost");
#db.setDatabaseName("DRIVER={SQL #Server};SERVER=localhost;DATABASE=playmates;UID=s a;PWD=the password");
db.setDatabaseName("pm");
db.setUserName("sa");
db.setPassword("the password");
f = db.open()
jacek
23rd June 2007, 15:09
In what directory the Qt DLLs that PyQt4 uses are located?
kikapu
23rd June 2007, 15:28
In what directory the Qt DLLs that PyQt4 uses are located?
Hmm...i am not sure i understand. If you mean the whole bunch of Qt dll, they are in c:\qt\4.2.3\bin
If you mean the .pyd that PyQt uses, it's at c:\Python2.5\lib\site-packages\PyQt4. There i see the QtCore.pyd, QtGui.pyd etc.
kikapu
23rd June 2007, 23:20
Hi jacek,
i found from here:
http://www.qtcentre.org/forum/p-qsqldatabase-qmysql-driver-not-loaded-post18045/postcount6.html
a post of yours to a folk that had the same problem and finally i have the solution:
I have to "create a QApplication first"...
When i put the line
app = QtGui.QApplication(sys.argv)
everything works as expected! :)
I cannot figure out why this is required but i will guess that plugins are loaded that way and thus is the recommened way to code it.
Also, i delete the qsqlodbc.dll fro \Qt\4.2.3\bin as it seems that is not needed. I'lhave to check if it is required by my program when i want to deploy it to another machine.
Thanks for the help!
jacek
24th June 2007, 00:52
I cannot figure out why this is required but i will guess that plugins are loaded that way and thus is the recommened way to code it.
QApplication (or QCoreApplication) is required for every Qt-based application, because it's resposible for initialization of the whole framework. One of the things it does is loading the plugins.
kikapu
25th June 2007, 12:18
Just out of curiocity :
The odbc entry i have made in Windows Control Panel "ODBC Data Source Administrator",is working ok when i set it to point at a database at my local host.
The code
db.setDatabaseName("pm");
is the only thing i need to connect to the server.
But when that odbc entry is point to a network server, it does not work. It requires the commands
db.setUserName("sa");
db.setPassword("password");
so it can work and connect.
I trapped the db.lastError() and it gives me :
[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection. QODBC3: Unable to connect
It seems that it somehow does not retain the credentials that i've correctly declared.
Am i missing something ??
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.