Results 1 to 3 of 3

Thread: ODBC and MS Access

  1. #1
    Join Date
    May 2006
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default ODBC and MS Access

    Sorry if this is a repost but I couldn't find another post on the subject. I have a need to use Qt and a MS Access database. I have the Qt ODBC plugin built. Now I am trying to access the database. I've done this using Qt and MySQL but I must be doing something wrong here I'm trying to keep it real simple. I wasn't sure what to put for the database name in setDatabaseName(). Do I put the name of the MS Access file?? I'm also not aware of any username/passwords. What am I missing? TIA.

    This is the output Im getting:
    ODBC:pen() : error opening connection : " [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified QODBC3: Unable to connect"

    Qt Code:
    1. int main(int argc, char ** argv)
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. QSqlDatabase mgDb = QSqlDatabase::addDatabase("QODBC");
    6.  
    7. qDebug() << "Library Paths " << QApplication::libraryPaths() << endl;
    8.  
    9. // Open connection.
    10. mgDb.setHostName("localhost");
    11. mgDb.setDatabaseName("MissileLogDB_USMTF.mda");
    12. if(mgDb.open() == false)
    13. {
    14. qDebug() << "ODBC::open() : error opening connection : "
    15. << mgDb.lastError().text() << "\n";
    16. return false;
    17. }
    18. return app.exec();
    19.  
    20. } // main
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2006
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: ODBC and MS Access

    OK guys, I figured out how to connect to my MS Access database using ODBC. You have to specify which driver your using for the ODBC connection type. If anyone cares here is the code to do that:

    Qt Code:
    1. QSqlDatabase mgDb = QSqlDatabase::addDatabase("QODBC");
    2. // Open connection.
    3. mgDb.setHostName("localhost");
    4. mgDb.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=MissileLogDB_USMTF.mda");
    To copy to clipboard, switch view to plain text mode 


    I can now get a listing of the tables, but I am having problems doing basic SQL type queries (i.e. SELECT * FROM table). I get an error stating: QODBCResult::reset: Unable to allocate statement handle Error:

    Here is the code:
    Qt Code:
    1. ///////////////////////////////////////////////////////////////////////////////
    2. int main(int argc, char ** argv)
    3. {
    4. QApplication app(argc, argv);
    5.  
    6. QSqlDatabase mgDb = QSqlDatabase::addDatabase("QODBC");
    7. QSqlQuery query;
    8.  
    9. // Open connection.
    10. mgDb.setHostName("localhost");
    11. mgDb.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=MissileLogDB_USMTF.mda");
    12.  
    13. if(mgDb.open() == false)
    14. {
    15. qDebug() << "ODBC::open() : error opening connection : "
    16. << mgDb.lastError().text() << "\n";
    17. }
    18.  
    19. QStringList list = mgDb.tables(QSql::Tables);
    20. for(int i=0;i<list.size(); ++i)
    21. {
    22. qDebug() << "Table names " << list.at(i) << endl;
    23. }
    24.  
    25. QSqlDriver const * driver = query.driver();
    26.  
    27. //This correctly returns the column indexes
    28. QSqlRecord rec = driver->record("EntityTypeTbl");
    29. int recNum = rec.indexOf("RecNum");
    30. int uniqueId = rec.indexOf("UniqueID");
    31. int sic = rec.indexOf("SIC");
    32. int msgType = rec.indexOf("MsgType");
    33.  
    34. //This is giving me problems
    35. if(!query.exec("SELECT * FROM EntityTypeTbl"))
    36. qDebug() << query.lastError().text();
    37.  
    38. // Enter Qt main thread's event loop.
    39. return app.exec();
    40. } // main
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: ODBC and MS Access

    I suggest you test you query on a qt4 tool before...
    or better convert your access to sqlite3 or many other format... so you can runn table on all OS.

    try to build this tool qt4:
    svn co https://qt-webdav.svn.sourceforge.ne.../odbc_convert/ odbc_convert

    cd odbc_convert/odbc_convert
    qmake && make

    set your access as odbc on a name XX like http://www.stylusstudio.com/docs/v2007/d_rdb20.html and connect simply via name XX
    on the window access driver 2006.

    is the same tool as $QTDIR/demos/sqlbrowser/ ++ i insert export dump to convert on other format.... ++ other help tool ...

Similar Threads

  1. Stored procedure and ODBC
    By filya in forum Qt Programming
    Replies: 2
    Last Post: 15th March 2010, 09:40
  2. Database access from Qt
    By nimmyj in forum General Discussion
    Replies: 1
    Last Post: 16th November 2006, 15:11
  3. Build qsqlodbc ODBC staticlib not possibel...
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 27th October 2006, 08:02
  4. Replies: 1
    Last Post: 4th October 2006, 17:05
  5. Can't install Qt4.1 with SQL Support for ODBC in Windows XP
    By danieltruchsess in forum Installation and Deployment
    Replies: 1
    Last Post: 5th February 2006, 11:02

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.