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]

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.
  • F Offline
    F Offline
    Fer_Bo
    wrote on 9 Aug 2015, 20:05 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 9 Aug 2015, 21:17 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

      F 1 Reply Last reply 9 Aug 2015, 23:58
      0
      • S SGaist
        9 Aug 2015, 21:17

        Hi and welcome to devnet,

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

        F Offline
        F Offline
        Fer_Bo
        wrote on 9 Aug 2015, 23:58 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
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 10 Aug 2015, 21:00 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

          F 1 Reply Last reply 10 Aug 2015, 23:38
          0
          • S SGaist
            10 Aug 2015, 21:00

            Can you show your complete db setup code ?

            F Offline
            F Offline
            Fer_Bo
            wrote on 10 Aug 2015, 23:38 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
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 16 Aug 2015, 22:22 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

              F 1 Reply Last reply 17 Aug 2015, 05:29
              0
              • S SGaist
                16 Aug 2015, 22:22

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

                See this bug and the related patch

                F Offline
                F Offline
                Fer_Bo
                wrote on 17 Aug 2015, 05:29 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
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 17 Aug 2015, 11:13 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

                  6/8

                  16 Aug 2015, 22:22

                  • Login

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