Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. How to connect MAMP with QT under OS X 10.10 [SOLVED]
Forum Update on Monday, May 27th 2025

How to connect MAMP with QT under OS X 10.10 [SOLVED]

Scheduled Pinned Locked Moved Installation and Deployment
osx yosemitemampmysql
8 Posts 2 Posters 2.2k 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.
  • Fer_BoF Offline
    Fer_BoF Offline
    Fer_Bo
    wrote on last edited by Fer_Bo
    #1

    Hi everyone!.
    I hope someone can help me, i'm a beginner; i installed MAMP and i try to connect a project in Qt with mySQL on my mac, when a i try to run a project i got this message: QSqlQuery::exec: database not open.

    db.setHostName("localhost");
    db.setPort(8889);
    db.setDatabaseName("books");
    db.setUserName("myUser");
    db.setPassword("password");

    I have checked the query and this right, but seems the connection never happens because the database is not open. Can be? that the name of the host is wrong?.

    Thanks! and i'm sorry for mi bad English, i'm from Mexico!.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Did you call db.open ? If so is returning true ? If not, what's the error message ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Fer_BoF 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Did you call db.open ? If so is returning true ? If not, what's the error message ?

        Fer_BoF Offline
        Fer_BoF Offline
        Fer_Bo
        wrote on last edited by
        #3

        @SGaist Yes, i put the function open.db in a if statement, if this is true, then a do the query, but the query never happens, because the error i got is the following: QSqlQuery::exec: database not open.
        The Query is also located in a if statement, for example:

        QSqlQuery myQuery;

        if (myQuery.exec("The SQL Query"))
        {
        Qdebug << "Success";
        }
        else
        Qdebug << "Error";

        Why the program sends me a message that the database is not open?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Can you show your complete db setup code ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Fer_BoF 1 Reply Last reply
          0
          • SGaistS SGaist

            Can you show your complete db setup code ?

            Fer_BoF Offline
            Fer_BoF Offline
            Fer_Bo
            wrote on last edited by
            #5

            @SGaist Yes, Here is:

            QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
            db.setHostName("localhost");
            db.setPort(8889);
            db.setDatabaseName("books");
            db.setUserName("myUser");
            db.setPassword("password");
            
            if(db.open())
            {
            
                QSqlQuery myQuery;
            
                if(myQuery.exec("SELECT AuthorID, LastName FROM Authors"))
                {
                    while(myQuery.next())
                    {
                        qDebug() << myQuery.value(1).toString();
                    }
                }
                else
                {
                    qDebug() << "No se completó la consulta";
                }
            
                qDebug() << "Cerrando base de datos...";
                db.close();
            }
            else
            {
                qDebug() << "Error = " << db.lastError().text();
            }
            
            
            return a.exec();
            

            }

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Currently there's a nasty little bug that makes open return always true with the QMYSQL plugin.

              See this bug and the related patch

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              Fer_BoF 1 Reply Last reply
              0
              • SGaistS SGaist

                Currently there's a nasty little bug that makes open return always true with the QMYSQL plugin.

                See this bug and the related patch

                Fer_BoF Offline
                Fer_BoF Offline
                Fer_Bo
                wrote on last edited by
                #7

                @SGaist Thank you for you help!!!, i solved my problem, i just added one line of code.

                db.setConnectOptions("UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock");

                Now I can do my queries with my database.

                Now, how can i mark this thread as SOLVED?, sorry i'm very new in this forum.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You're welcome !

                  If you can't from the Topic Tools button, just edit the thread title and prepend [solved] and you're good

                  Happy coding !

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1

                  • Login

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