B
B
BogBel2015-05-13 18:04:01
MySQL
BogBel, 2015-05-13 18:04:01

How to properly connect MySQL to Qt5.4?

The problem is quite hackneyed, but still, I ask for help, I'm stuck in this very much.
Downloaded installed.
After the operations that were not done quite accurately, due to the inaccuracy of the instructions, I decided to compile a code example.
I get that there seems to be a driver for QMYSQL, but I still get an error.
I would be incredibly grateful for step-by-step instructions on how to fix and still connect the DBMS to Qt.

//main.cpp
#include "widget.h"
#include <QApplication>
#include<QSqlDatabase>
#include<QSql>
#include<QDebug>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    qDebug() << QSqlDatabase::drivers();
    Widget w;
    w.show();

    return a.exec();
}

//widget.cpp конструктор
#include "widget.h"
#include "ui_widget.h"
#include<QDebug>
#include<QSql>
#include<iterator>
#include<QSqlDatabase>
#include<QSqlQuery>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("mysql");
    db.setUserName("root");
    db.setPassword("rootPW");
    if (!db.open()) qDebug() << "Failed to connect to root mysql admin";
    ui->textEdit->setReadOnly(true);

}

as a result, something like this is output to the console:
Running D:\Qt projects\build-History-Desktop_Qt_5_4_1_MinGW_32bit-Debug\debug\History.exe...
("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC" , "QODBC3", "QPSQL", "QPSQL7")
Failed to connect to root mysql admin

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question