Connecting to MySQL



  • Here is a simple example of connecting to the database:
    @#include <QCoreApplication>
    #include <QDebug>
    #include <QtSql/QSqlDatabase>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "autoservise");
    db.setHostName("localhost");
    db.setDatabaseName("autoservise");
    db.setUserName("root");
    db.setPassword("pass");
    return a.exec&#40;&#41;;
    

    }@

    The output is: qmysql driver not loaded. But the driver is active.

    Did everything as written in the documentation.

    Turned four files:

    libqsqlmysql.a
    libqsqlmysqld.a
    libqsqlmysql.dll
    libqsqlmysqld.dll

    Throws files in a folder: E:\qt\Qt5.3.1\5.3\mingw482_32\plugins\sqldrivers. And the result is the same. What could be the problem?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Do you have the MySQL client library installed ?



  • Yes. I installed all the libraries that were offered during the server installation.



  • Hi
    In my project i did this,

    @

    db = QSqlDatabase::addDatabase("QODBC");
    db.setHostName("localHost");
    db.setPort(3306);
    db.setDatabaseName("DRIVER={MySQL ODBC 5.2w Driver};DATABASE={joint_insp};UID={root}");
    db.setUserName("root");
    db.setPassword("root");
    
    if(!db.open())
        showErrorDialog("Database Not Opened");
    

    @

    for this you need to install ODBC driver, or it my be already installed in your system(with mysql installation). You can check in "Control Panel\All Control Panel Items\Administrative Tools\Data Sources(ODBC)" and in drivers tab.


  • Lifetime Qt Champion

    So next step is to let your program know where the MySQL client libraries are.

    In the Run part of the Project panel, edit the PATH variable and the the path to the folder where the MySQL client libraries can be found



  • "So next step is to let your program know where the MySQL client libraries are.
    In the Run part of the Project panel, edit the PATH variable and the the path to the folder where the MySQL client libraries can be found"

    It did not help. Writes:

    QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7


  • Lifetime Qt Champion

    Did you check what error message is returned ?


Log in to reply
 

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