Parsing .mdb File in Qt4



  • Here's what I am trying to do:
    I have multiple .mdb files on my desktop containing 3 tables in each with specific data that I need. I would like to parse these tables, and get data from them. Is there a way to do this in Qt4/c++ libraries?

    My overall goal:
    I would like to take this data, and plot it in a Qwt plot (which I have already played around with already).

    Thanks in advance.



  • Your best bet would be to use ODBC via de QtSql module, and connect to the .mdb files as a database. That approach works quite well, I have used it a lot in my previous job.



  • Thank you for your quick reply. I thank you for your input and will check it out.

    Do you just connect as a localhost and query the file "or database"?



  • you will use the file as a database (use SQL to query data).

    Here a small code to connect to a mdb file using ODBC

    @QString filePath = "DRIVER={Driver do Microsoft Access (*.mdb)};FIL={CONN_NAME};DBQ=file.mdb";
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "odbc");
    db.setDatabaseName(filePath);
    QSqlQuery query(db)
    query.exec("SELECT * FROM TABLE;")
    ...
    @



  • Thank you for the helpful sample code, I will take a look and see if I can make some progress.

    Thanks again!



  • I apologize for the double post, for some reason I cannot get your code snipet to work. I have the odbc driver installed on linux, and i have the QtSql/SqlDatabae, QtSql/QODBCDriver incuded. I get the error when I try to run "sql.h: no such file or directory".

    @

    #include <QtCore>
    #include <QtSql/QSqlDatabase>
    #include <QtSql/QSqlQuery>
    #include <QtSql/QODBCDriver>
    #include <QtSql/qsql.h>
    #include "globals.h"
    #include "graph.h"
    #include "graphscrollbar.h"
    #include "buttonheader.h"
    #include "buttonsidebar.h"
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    // file prototype
    void connectODBCdatabase(QString selectedfile);

    void connectODBCdatabase(QString selectedfile)
    {
    // build database string
    QString filePath = "DRIVER={Driver do Microsoft Access (*.mdb)};FIL={CONN_NAME};DBQ=";
    filePath.append(selectedfile);

    // create database
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "odbc");
    db.setDatabaseName(filePath);
    
    // get data from .mdb file
    QSqlQuery query(db);
    query.exec("SELECT * FROM TABLE tablename;"&#41;;
    

    }

    @



  • On linux, you are probably out of luck. AFAIK, there is no ODBC driver for mdb on non-windows systems. If you are on linux, I would strongly encourage you to see if there isn't a way to switch to a different database format.



  • Unfortunately there is a separate software suit that creates the .mdb files, which is out of my control at the moment. I thought the unixODBC driver allows the user to communicate to that specific database format, which in turn, should allow me to access the database file correct? I guess my understanding on .mdb files is vague though...

    I could convert the file to a comma delimited file using tools already available in the Linux world, but lets be real here, that's unacceptable to my standards :) I would like to automate this process.

    There's gotta be a way to connect using SQL query's to the file.

    I thank you for your insight, I will keep searching around.



  • [quote author="Andre" date="1313780603"]On linux, you are probably out of luck. AFAIK, there is no ODBC driver for mdb on non-windows systems.[/quote]
    yes, does exist, but it is paid http://www.easysoft.com/products/data_access/odbc-access-driver/

    You can use mdb tools to extract the data into a csv and parse as plain text (http://mdbtools.sourceforge.net/)



  • yeah, that was my "easy way out" using mdb tools. I fooled around with it yesterday, and was satisfied, yet I still would like to not have to convert the file. I will check out easysoft. Thank you.



  • ODBC in and off itself does not allow you to interact with any database. It needs drivers to do the actual work. -AFAIK, there is no ODBC driver for .mdb files on unix, but I may be off there. I never found something that worked.-



  • [quote author="Wagner Reck" date="1313781485"]
    [quote author="Andre" date="1313780603"]On linux, you are probably out of luck. AFAIK, there is no ODBC driver for mdb on non-windows systems.[/quote]
    yes, does exist, but it is paid http://www.easysoft.com/products/data_access/odbc-access-driver/

    You can use mdb tools to extract the data into a csv and parse as plain text (http://mdbtools.sourceforge.net/)

    [/quote]

    I stand corrected. Thanks for the information!



  • yeah, that was my "easy way out" using mdb tools. I fooled around with it yesterday, and was satisfied, yet I still would like to not have to convert the file. I will check out easysoft, yet its quite a hefty price unfortunately. Thank you though.



  • Not sure if this is an option, but...
    Could you keep the actual files on a windows machine and connect to that machine over the network? If so, than it should be doable to either expose the ODBC data connection to the network, or perhaps create a Qt based server that does the actual data querying for its connected (linux) clients.



  • Now you got my thinkin.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.