P
P
Philip Bondarev2017-10-19 11:29:30
MySQL
Philip Bondarev, 2017-10-19 11:29:30

How to connect to MySQL from Qt?

I re-read and reviewed a bunch of material about connecting to MySQL from Qt . MySQL version - 5.5 , Qt - 5.9.1 , compiler mingw53_32 , system - Win10x64 .
C :\Qt\Qt5.9.1\5.9.1\mingw53_32\plugins\sqldrivers already contains qsqlmysql.dll and qsqlmysqld.dll , so there is no point in compiling them from source, right?
If so, why does the notorious one pop up:

("C:/Qt/Qt5.9.1/5.9.1/mingw53_32/plugins", "D:/CPP/Qt/build-BlackWoodWhip-Desktop_Qt_5_9_1_MinGW_32bit-Debug/debug")
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
Error

At the same time, I copied libmysql.dll and libmysql.lib from C:\Program Files\MySQL\MySQL Server 5.5\lib wherever possible and impossible (project folder, folder with the debug-version executable, Windows, folder C:\Qt\Qt5. 9.1\5.9.1\mingw53_32\plugins\sqldrivers ), files qsqlmysql.dll and qsqlmysqld.dll , are also shoved everywhere. Here is my PATH :
PATH

C:\Program Files\ImageMagick-7.0.6-Q16;D:\Python27\;
D:\Python27\Scripts;C:\ProgramData\Oracle\Java\javapath;
C:\Windows\system32;
C:\Windows;
C:\Windows\System32\Wbem;
C:\Windows\System32\WindowsPowerShell\v1.0\;
C:\PROGRA~2\NVIDIA~1\PhysX\Common;
D:\Programs\Git\cmd;
D:\Python27\Lib\site-packages;
D:\Python27\Lib\site-packages\PyQt5;
D:\Python27\include;
D:\Python27\libs;
D:\Programs\TortoiseSVN\bin;
D:\Go\bin;D:\Programs\nodejs\;
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;
C:\Program Files (x86)\Pico Technology\PicoScope6\
;C:\Program Files (x86)\Xoreax\IncrediBuild;
C:\Qt\Qt5.9.1\5.9.1\msvc2015\bin;
C:\PROGRA~2\MICROS~2.0\VC\bin;
C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools;
C:\Program Files (x86)\Windows Kits\10\Lib\10.0.15063.0\um\x86;
C:\Qt\Qt5.9.1\5.9.1\mingw53_32\plugins\sqldrivers;
C:\Program Files\MySQL\MySQL Server 5.5\lib;
C:\Program Files\MySQL\MySQL Server 5.5\bin;

Here is the PATH from QtCreator Build Environment :
PATH Build Environment

C:\Qt\Qt5.9.1\5.9.1\mingw53_32\bin;
C:\Qt\Qt5.9.1\Tools\mingw530_32\bin;
C:\Qt\Qt5.9.1\5.9.1\mingw53_32\plugins\sqldrivers;
C:\Program Files\MySQL\MySQL Server 5.5\lib;
C:\Program Files\MySQL\MySQL Server 5.5\bin;

Here is the .pro file:
.pro
QT       += core gui sql

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = BlackWoodWhip
TEMPLATE = app

DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui

RESOURCES += \
    icons/IcoAll.qrc \
    icons/gui.qrc

CONFIG (debug, debug|release) {
    CONFIG       += console
}


Here is main.cpp , mainwindow.h and mainwindow.cpp
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QStyleFactory>

int main(int argc, char *argv[])
{
    QStringList paths = QCoreApplication::libraryPaths();
    paths.append(".");
    paths.append("imageformats");
    paths.append("platforms");
    paths.append("sqldrivers");
    QCoreApplication::setLibraryPaths(paths);

    QApplication::setOrganizationName( "FILTR-KTV" );
    QApplication::setOrganizationDomain( "filter-tv.ru" );
    QApplication::setApplicationName( "BlackWoodWhip" );

    QApplication a(argc, argv);
    QApplication::setStyle (QStyleFactory::create ("Fusion"));
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlTableModel>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QSqlDatabase db;
};

#endif // MAINWINDOW_H

mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QtSql>
#include <QSqlError>
#include <QSqlRecord>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    qDebug() << QApplication::libraryPaths();

    //DATABASE MySQL
    db = QSqlDatabase::addDatabase("QMYSQL", "BWW");
    db.setHostName("127.0.0.1");
    db.setDatabaseName("blackwoodwhip");
    db.setUserName("root");
    db.setPassword("root");
    if (!db.open()){
        qDebug() << "Error";
    } else {
        qDebug() << "Ok";
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}


Now, attention to the question, how to be in such a situation?
I tried re-compiling the .dll 's from the sources, but ran into some errors (seems to be complaining that it doesn't mean what mysql is .

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2017-10-19
@Mercury13

Two DLLs are needed: libmysql.dll in the program directory, and qsqlmysql.dll in the plugins/sqldrivers directory.
You can get rid of the plugins subdirectory by writing in the program

QApplication a(argc, argv);   // это наша программа, обычно создано автоматом
a.addLibraryPath(QCoreApplication::applicationDirPath());

From sqldrivers, as far as I know, this is not possible.

P
Pavel, 2017-10-19
@electronik777

use

if (!db.open()){
        qDebug() << QObject::trUtf8("Database error connect") << db.lastError().text();
    }

you will see an error there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question