Important: Please read the Qt Code of Conduct -

[Solved] How to "Qt 4.8 + MySQL with ODBC on Windows"?

  • Hi everyone. I’ve been reading other posts about this, but they either end up talking about Linux or Mac or they don’t get any solutions.

    I’m trying to make a simple testing app with Qt 4.8.6 that connects to a database.. I just want the database connection to work since I’m not exactly new at Qt. The problem is that I tried to use QMYSQL driver but I don’t have it available and building it won’t be that handy for what I need.

    This is the code I use to create and open the DB connection:

    @ db = QSqlDatabase::addDatabase("QODBC");
    if (!
    QMessageBox::critical(0, QObject::tr("Database Error"),


    And the db.lastError() reports:

    “[Microsoft][ODBC Driver manager]Data source name not found and no default driver have been specified
    QODBC3: Unable to connect”
    (My translation may not be the same as the Windows english version original message)


    I also have:

    @ Qt += sql@

    MySQL server is running

    Can you point me in the right direction please? What could be wrong?

  • -Hi, try

    EDIT: Sorry, my bad, I thought it was a SQLServer :-(
    Must be getting old.
    Indeed for MySQL you need a DSN.

  • You need to setup a DSN, there's already a thread regarding ODBC-connection:

  • Hi, thanks for your replies.

    I've already set a system DSN. But just to be sure...

    Is Xampp's MySQL service enough for this? Or MySQL Server is necessary?

    I have both on my machine, but only Xampp's is up.

  • Is MySQL up? If so, you need to set the DSN in your connection string.

    Short example
    db = QSqlDatabase::addDatabase("QODBC");
    db.setDatabaseName("Driver={MySQL ODBC 5.1 Driver};DATABASE=agp-dbserver01;");

    Or try like this:

    db.setDatabaseName("Driver={MySQL ODBC 5.1 Driver};USER=youruser;Password=yourpw;SERVER=;");

    I'm also sure you will find good examples.

    Have a nice weekend!

  • You were right @jensen82. I had to set a DSN with the same name of the database I wished to connect to... I don't know if there's an especific reason for this, as well as if I need to use MySQL server and not Xampp's MySQL service...

    Anyway, this post is now solved :)

    Thanks to you both..!

  • Hi BlastDV,

    Please add <solved> in to your title.


  • Thanks for reminding me @ankursaxena!

    By the way, I forgot to mention, I tested it too with Xampp's MySQL service and it worked fine! I don't know if there will be any problem with it but I tested it with some querys and it does what I expected to...

  • The only problem with ODBC-Connections that you have to setup a DSN. With the driver/plugin for Qt you can use MySQL without DSN-Setup.

  • Do you mean the QMYSQL driver? I was reading something about building it but right now I don't really have time to get into that since I'm on a little rush to finish my project, and the ODBC's DSN setup wont be a problem for it.

    But yeah, I'll learn to do that as well as migrating to Qt 5 :D

Log in to reply