qt5.1.1 mysql ubuntu QMYSQL driver not loaded

[亡魂溺海] 提交于 2019-12-18 03:43:27

问题


I'm trying to access mysql using Qt5.1.1 but i am getting the error the error below. I also searched a lot on google but unable to fix it.Please suggest me a solution so that i am able to resolve this error.

error:

QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3
QSqlError(-1, “ driver not loaded”, “ driver not loaded”)

code:

#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QtSql>
#include <QSqlDriver>
#include <qsqldatabase.h>
#include <QSqlError>
#include <QPluginLoader>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QSqlDatabase db= QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("NNF");
    db.setUserName("root");
    db.setPassword("root123");
    if( !db.open() )
    {
        qDebug() << db.lastError();
        qFatal( "Failed to connect." );
    }

    qDebug( "Connected!" );
    return a.exec();
}

回答1:


If you are Ubuntu linux OS, you can install library:

mic@ubt: ~$ apt-cache search libqt4-sql-mysql
libqt4-sql-mysql - Qt 4 MySQL database driver

mic@ubt: ~$ sudo apt-get install libqt4-sql-mysql 



回答2:


Well, for Qt5 you need install MySQL, using the next command on the terminal, you resolve the problem:

sudo apt-get install libqt5sql5-mysql


来源:https://stackoverflow.com/questions/21476529/qt5-1-1-mysql-ubuntu-qmysql-driver-not-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!