E
E
Egorithm2017-02-04 21:13:30
Qt
Egorithm, 2017-02-04 21:13:30

Qt. How to connect a db?

Unable to connect database

#include <QtWidgets>
#include <QtSql>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication App(argc,argv);

    MainWindow *Window = new MainWindow;
    Window->show();

    QSqlDatabase db = QsqlDatabase::addDatabase("QMYSQL","mydb");
    db.setHostName("localhost");
    db.setDatabaseName("institute");
    db.setUserName("root");
    db.setPassword("1234");
    bool ok = db.open();

    int result = App.exec();
    delete Window;

    return result;
}

Code taken from here: Development → Working with databases in Qt .
Here is the compiler message:
A:\QtProjects\Lab_Part_2\main.cpp:12: ошибка: 'QsqlDatabase' has not been declared
     QSqlDatabase db = QsqlDatabase::addDatabase("QMYSQL","mydb");
                       ^

QT += sqldid, Ctrl+Click refers to qsqldatabase.h

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Romanov, 2017-02-04
@EgoRusMarch

'QsqlDatabase
'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question