Results 1 to 10 of 10

Thread: Connecting to an ODBC Data Source

  1. #1
    Join Date
    May 2007
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Connecting to an ODBC Data Source

    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!
    Software Engineer, riding a Kona King Kikapu 2007

  2. #2
    Join Date
    May 2007
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting to an ODBC Data Source

    Anyone ??

    Is it something to do with the OpenSource version of Qt that i use ??
    Software Engineer, riding a Kona King Kikapu 2007

  3. #3
    Join Date
    May 2007
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting to an ODBC Data Source

    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 ?

    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...
    Last edited by kikapu; 23rd June 2007 at 12:33.
    Software Engineer, riding a Kona King Kikapu 2007

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting to an ODBC Data Source

    Can you see QODBC driver when you start the sqlbrowser demo?

  5. #5
    Join Date
    May 2007
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting to an ODBC Data Source

    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...)

    Qt Code:
    1. from PyQt4 import QtCore, QtSql
    2.  
    3. db = QtSql.QSqlDatabase.addDatabase("QODBC");
    4. db.setHostName("localhost");
    5. #db.setDatabaseName("DRIVER={SQL #Server};SERVER=localhost;DATABASE=playmates;UID=sa;PWD=the password");
    6. db.setDatabaseName("pm");
    7. db.setUserName("sa");
    8. db.setPassword("the password");
    9. f = db.open()
    To copy to clipboard, switch view to plain text mode 
    Software Engineer, riding a Kona King Kikapu 2007

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting to an ODBC Data Source

    In what directory the Qt DLLs that PyQt4 uses are located?

  7. The following user says thank you to jacek for this useful post:

    kikapu (23rd June 2007)

  8. #7
    Join Date
    May 2007
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting to an ODBC Data Source

    Quote Originally Posted by jacek View Post
    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.
    Software Engineer, riding a Kona King Kikapu 2007

  9. #8
    Join Date
    May 2007
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting to an ODBC Data Source

    Hi jacek,

    i found from here:
    http://www.qtcentre.org/forum/p-qsql...ostcount6.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

    Qt Code:
    1. app = QtGui.QApplication(sys.argv)
    To copy to clipboard, switch view to plain text mode 

    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!
    Software Engineer, riding a Kona King Kikapu 2007

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting to an ODBC Data Source

    Quote Originally Posted by kikapu View Post
    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.

  11. #10
    Join Date
    May 2007
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting to an ODBC Data Source

    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

    Qt Code:
    1. db.setDatabaseName("pm");
    To copy to clipboard, switch view to plain text mode 

    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

    Qt Code:
    1. db.setUserName("sa");
    2. db.setPassword("password");
    To copy to clipboard, switch view to plain text mode 

    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 ??
    Software Engineer, riding a Kona King Kikapu 2007

Similar Threads

  1. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.