Results 1 to 3 of 3

Thread: QODBC connection string failed

  1. #1
    Join Date
    Sep 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QODBC connection string failed

    Hi All,
    I was using QODBC and set up a DSN to access a Microsoft Access (.mdb) database without any problem. My Qt version is 4.6.3. Now I would like to deploy the app. and avoid requiring users to set up a DSN. I found out that you can use a connection string instead. Please search for "connection string" in below page.
    http://doc.qt.nokia.com/4.6/qsqldata...l#QSqlDatabase

    Here is the DSN based code.

    //----Initialize Database----
    _dbDriver = "QODBC";
    _dbDSN = "DecisionMakingStudy";
    _dbHost = "localhost";
    _dbUser = "";
    _dbPasswd = "";

    _db = QSqlDatabase::addDatabase(_dbDriver);
    _db.setConnectOptions("SQL_ATTR_ODBC_VERSION=SQL_O V_ODBC3");//force ODBC3.x behavior
    _db.setHostName(_dbHost);
    _db.setUserName(_dbUser);
    _db.setPassword(_dbPasswd);
    _db.setDatabaseName(_dbDSN);

    if(!_db.open()){
    _db.setConnectOptions();// clear options
    qFatal(_db.lastError().text());
    }

    ...

    Here is the connection string based code which is *failed* in _db.open() with error
    "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified QODBC3: Unable to connect"

    _db = QSqlDatabase::addDatabase(_dbDriver);
    _db.setConnectOptions("SQL_ATTR_ODBC_VERSION=SQL_O V_ODBC3");//force ODBC3.x behavior
    QString databaseName = "DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};UID=;PWD=;DBQ=test.mdb";
    _db.setDatabaseName(databaseName);

    if(!_db.open()){
    _db.setConnectOptions();// clear options
    qFatal(_db.lastError().text());
    }
    ...

    The only difference between the two methods is highlighted in red. The .mdb file located at the project root directory. Using the absolute file path in connection string method did not help. Also connecting to a copy of the DSN linked .mdb (rather than the linked .mdb itself worrying DSN setup might prevent external access) failed as well.

    Would you please help? Thanks so much.

    Best,
    dm

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    84
    Thanks
    7
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QODBC connection string failed

    this worked for me:
    Qt Code:
    1. _db = new QSqlDatabase(QSqlDatabase::addDatabase("QODBC3","selections") );
    2.  
    3. QString connect_str = "DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS Access};DBQ=";
    4. // "DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=";
    5.  
    6. QString str_db;
    7.  
    8. str_db = connect_str + fn;
    9. // fn = filename
    10.  
    11. _db->setDatabaseName(str_db);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QODBC connection string failed

    Hi jh,

    Thanks a lot. It works!
    I guess the trick resides in the DRIVER value. Here is mine (failed)
    DRIVER={Microsoft Access Driver (*.mdb)};
    Here is yours (successful):
    DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};
    even though my database file is a .mdb. Here is the quote from
    http://doc.qt.nokia.com/4.6/qsqldata...l#QSqlDatabase
    "For example, Microsoft Access users can use the following connection string to open an .mdb file directly, instead of having to create a DSN entry in the ODBC manager:

    ...
    db = QSqlDatabase::addDatabase("QODBC");
    db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=myaccessfile.mdb");
    if (db.open()) {
    // success!
    }
    ...
    There is no default value.
    ..."
    I will report this as a bug.
    Thanks again for the help.
    Best,
    dm

Similar Threads

  1. SSL with QODBC ??!
    By codeman in forum Qt Programming
    Replies: 0
    Last Post: 21st September 2010, 10:01
  2. Replies: 3
    Last Post: 8th April 2010, 21:42
  3. Replies: 1
    Last Post: 2nd April 2010, 07:42
  4. QODBC on BLOB
    By baray98 in forum Qt Programming
    Replies: 0
    Last Post: 7th November 2009, 00:26
  5. QPSQL and failed connection
    By quickNitin in forum Newbie
    Replies: 4
    Last Post: 16th January 2007, 12:18

Tags for this Thread

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.