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



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


  • Lifetime Qt Champion

    Hi and welcome to devnet,

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



  • @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?


  • Lifetime Qt Champion

    Can you show your complete db setup code ?



  • @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();
    

    }


  • Lifetime Qt Champion

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

    See this bug and the related patch



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


  • Lifetime Qt Champion

    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 !


Log in to reply
 

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