Answer the question
In order to leave comments, you need to log in
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
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
}
#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();
}
#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
#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;
}
Answer the question
In order to leave comments, you need to log in
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());
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question