Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] How to "Qt 4.8 + MySQL with ODBC on Windows"?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 4.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BlastDV
    wrote on last edited by
    #1

    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");
    db.setHostName("localhost");
    db.setDatabaseName("test");
    db.setUserName("compusciens");
    db.setPassword("kakolukiya");
    db.setPort(3306);
    if (!db.open())
    {
    QMessageBox::critical(0, QObject::tr("Database Error"),
    db.lastError().text());

    }
    

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

    Notes:

    I also have:

    @ Qt += sql@

    MySQL server is running

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

    (8) Just live your life blind like me (8)

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      -Hi, try
      xxxx

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

      1 Reply Last reply
      0
      • jensen82J Offline
        jensen82J Offline
        jensen82
        wrote on last edited by
        #3

        You need to setup a DSN, there's already a thread regarding ODBC-connection: https://qt-project.org/forums/viewthread/3478

        1 Reply Last reply
        0
        • B Offline
          B Offline
          BlastDV
          wrote on last edited by
          #4

          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.

          (8) Just live your life blind like me (8)

          1 Reply Last reply
          0
          • jensen82J Offline
            jensen82J Offline
            jensen82
            wrote on last edited by
            #5

            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=127.0.0.1;");
            @

            I'm also sure you will find good examples.

            Have a nice weekend!

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BlastDV
              wrote on last edited by
              #6

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

              (8) Just live your life blind like me (8)

              1 Reply Last reply
              0
              • A Offline
                A Offline
                ankursaxena
                wrote on last edited by
                #7

                Hi BlastDV,

                Please add <solved> in to your title.

                Thanx.

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  BlastDV
                  wrote on last edited by
                  #8

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

                  (8) Just live your life blind like me (8)

                  1 Reply Last reply
                  0
                  • jensen82J Offline
                    jensen82J Offline
                    jensen82
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      BlastDV
                      wrote on last edited by
                      #10

                      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

                      (8) Just live your life blind like me (8)

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved