[Solved]QSqlDatabasd and undefined reference issue



  • Hi guys ,
    I ùm trying to connet to mysql database but i get a list of undefined reference when i compile the project.
    Here's my code
    @#ifndef DATABASE_H
    #define DATABASE_H

    #include <QtSql/QSqlDatabase>
    #include <QtSql/QSqlError>
    #include <QDebug>

    class database
    {
    public:
    database(/QString username,QString password,QString host/);
    static bool createConnection();
    private:
    QString username;
    QString password;
    QString host;
    };

    #endif // DATABASE_H

    #include "database.h"

    database::database()
    {
    }
    static bool createConnection(){
    QSqlDatabase db =QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("mysql");
    db.setUserName("root");
    db.setPassword("morpheus2011");
    if (!db.open()){qDebug() << "Failed to connect to root mysql admin";return false;}
    else return true;
    }

    here's the undefined reference list:
    /home/aladin/Projects/Qt/pfe/database.cpp:7: error: undefined reference to QSqlDatabase::defaultConnection' /home/aladin/Projects/Qt/pfe/database.cpp:7: error: undefined reference toQSqlDatabase::addDatabase(QString const&, QString const&)'
    /home/aladin/Projects/Qt/pfe/database.cpp:8: error: undefined reference to `QSqlDatabase::setHostName(QString const&)'
    the list is long :)

    @
    can anyone help me ?

    Thanks in advance.



  • OK , i solved that added this line #include <QtSql> to database.h and QT +=sql to .pro file but when i run the program i get a new error :
    QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE
    Failed to connect to root mysql admin
    false

    how to solve that ?


  • Moderators

    click on the mysql tag on the right of your screen.
    You will find several explanations on how to get the mysql driver on different operating systems.

    or use this link:
    http://developer.qt.nokia.com/search/tag/mysql



  • the problem was solved , i have two installation of Qt , i used the one installed from repository (ubuntu ) and it works fine .
    thanks for your help .


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.