Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

error connecting to Mysql



  • good afternoon.
    I have a question regarding database, I have a database made in mysql, and when I want to connect from qt, I get an error message, that the controller was not found, and when I see the message it comes out that they are only available the sqlite, postgre and obdc drivers, maybe in the version of qt that I am using that driver is no longer available, I am using version 5.13.1 of qt, I would appreciate your comments, I leave the code I made to connect to the base of data, I also leave a screenshot of the error message.

    codigo:

    #include "principalclass.h"
    #include "ui_principalclass.h"
    #include <QSqlDatabase>
    #include <QSqlError>
    #include <QMessageBox>
    
    PrincipalClass::PrincipalClass(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::PrincipalClass)
    {
        ui->setupUi(this);
        QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
        if(!db.isDriverAvailable("QMYSQL"))
          {
            QMessageBox::critical(this,qApp->applicationName(),
                                  "driver was not found.\n"+db.lastError().text());
            return;
          }
        db.setPort(3306);
        db.setHostName("localhost");
        db.setDatabaseName("ejemplo");
        db.setUserName("root");
        db.setPassword("2311046");
        if(!db.open())
          {
            QMessageBox::critical(this,qApp->applicationName(),
                                  "error opening database.\n"+db.lastError().text());
            return;
          }
        QMessageBox::information(this,qApp->applicationName(),
                                 "Todo bien");
    }
    
    PrincipalClass::~PrincipalClass()
    {
        delete ui;
    }
    
    
    

    0_1568326831093_55308d63-b8ab-43b1-acaa-b5663b3c0432-image.png



  • @lincoln
    You are not configured yet to use MySQL. This comes up so often here I think there may be post, search this forum or Google for the message you see QSqlDatabase QMYSQL driver not loaded, pick a recent-ish answer, there are several. If you have to build it yourself, and you're Windows, the main stickied thread at https://forum.qt.io/topic/40672/how-to-create-the-plugin-of-mysql-for-qt may be relevant, but I think you can get it pre-compiled for Windows from somewhere.



  • Well I take it back, my question would be, how to build the mysql driver in qt 5.13.1, I already tried to do it with the official QT documentation but I didn't get it.
    I did this

    0_1568351348652_23541bcd-551b-4733-b147-dbeec9232a5a-image.png

    0_1568351412435_1bff645f-a0ef-4d21-bde7-b91afe424234-image.png

    and there was no result, I get this.


  • Qt Champions 2019

    Move the mysql libs to a path without spaces and try again. Or properly quote your paths or use MYSQL_INCDIR/MYSQL_LIBDIR as stated in the documentation



  • Well friends, I tried everything and there is no result, thanks anyway.


  • Qt Champions 2019

    @lincoln said in error connecting to Mysql:

    Well friends, I tried everything and there is no result, thanks anyway.

    What did you do? You will at least get another error message than the one above.



  • I tried an ODBC connection but I also got an error, so I think I'll leave it there, I will try PostGresql.


  • Qt Champions 2019

    @lincoln said in error connecting to Mysql:

    I tried an ODBC connection

    So you never tested our suggestions... good to know :(



  • @christian-ehrlicher said in error connecting to Mysql:

    MYSQL_INCDIR/MYSQL_LIBDIR

    I did what they told me, but I got more errors than in the beginning, I don't even compile when I switch to MYSQL_INCDIR / MYSQL_LIBDIR, as you said, so I left it like that, I have to move forward with this since it is a job for the university, I thank you for your comments.


Log in to reply