Results 1 to 3 of 3

Thread: QSqlDatabase driver problem

  1. #1
    Join Date
    Oct 2008
    Location
    Brasil - São Paulo - Marília
    Posts
    28
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    3

    Default QSqlDatabase driver problem

    Hi all!

    I have the following code:

    Qt Code:
    1. #ifndef QMYSQLCONNECTION_H
    2. #define QMYSQLCONNECTION_H
    3.  
    4. #include <QObject>
    5. #include <QSqlDatabase>
    6.  
    7. class QMysqlConnection: public QObject, protected QSqlDatabase
    8. {
    9. Q_OBJECT
    10.  
    11. friend class QMysqlTable;
    12.  
    13. public:
    14. QMysqlConnection(const QString& host, const QString& user, const QString&
    15. password, const QString database, QObject* = 0);
    16.  
    17. bool isOpen(void) const;
    18. void close(void);
    19. };
    20.  
    21. #endif // QMYSQLCONNECTION_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QDebug>
    2. #include <QSqlError>
    3.  
    4. #include "QMysqlConnection.h"
    5.  
    6. // Public functions
    7. QMysqlConnection::QMysqlConnection(const QString& host, const QString& user,
    8. const QString& password, const QString database, QObject* parent):
    9. QObject(parent)
    10. {
    11. this->addDatabase("QMYSQL");
    12.  
    13. this->setHostName(host);
    14. this->setUserName(user);
    15. this->setPassword(password);
    16. this->setDatabaseName(database);
    17.  
    18. this->open();
    19.  
    20. qDebug() << "Last connection error = " << this->lastError();
    21. }
    22.  
    23. bool QMysqlConnection::isOpen(void) const
    24. {
    25. return QSqlDatabase::isOpen();
    26. }
    27.  
    28. void QMysqlConnection::close(void)
    29. {
    30. QSqlDatabase::close();
    31. }
    To copy to clipboard, switch view to plain text mode 

    This returns me the following error:
    QSqlError(-1, "Driver not loaded", "Driver not loaded")

    But if I change this code
    Qt Code:
    1. this->addDatabase("QMYSQL");
    2.  
    3. this->setHostName(host);
    4. this->setUserName(user);
    5. this->setPassword(password);
    6. this->setDatabaseName(database);
    7.  
    8. this->open();
    To copy to clipboard, switch view to plain text mode 
    to this code:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    2.  
    3. db.setHostName(host);
    4. db.setUserName(user);
    5. db.setPassword(password);
    6. db.setDatabaseName(database);
    7.  
    8. db.open();
    To copy to clipboard, switch view to plain text mode 

    it works! Why this code works and the other code not?

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlDatabase driver problem

    addDatabase returns a QSqlDatabase object.
    You'd have to assign it to *this.
    (Your own object still has/is/uses a default-constructed (i.e. not initialized) QSqlDatabase.)

    HTH

  3. The following user says thank you to caduel for this useful post:

    croscato (21st November 2008)

  4. #3
    Join Date
    Oct 2008
    Location
    Brasil - São Paulo - Marília
    Posts
    28
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    3

    Default Re: QSqlDatabase driver problem

    A used the following code and the problem was solved:

    Qt Code:
    1. QMysqlConnection::QMysqlConnection(const QString& host, const QString& user, const QString& password, const QString database, QObject* parent):
    2. QObject(parent), QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"))
    To copy to clipboard, switch view to plain text mode 

    Thanks...
    Last edited by croscato; 21st November 2008 at 16:23.

Similar Threads

  1. QSqlDatabase: QMYSQL driver not loaded
    By onder in forum Newbie
    Replies: 12
    Last Post: 29th March 2017, 14:43
  2. Replies: 1
    Last Post: 7th July 2008, 20:13
  3. Problem compiling ibase driver
    By SteM in forum Installation and Deployment
    Replies: 12
    Last Post: 24th September 2007, 22:26
  4. QTimer and QSqlDatabase Problem
    By smtgra011 in forum Qt Programming
    Replies: 6
    Last Post: 4th July 2007, 16:37
  5. My Mysql 5 and Qt 4.2.2 Problem (Driver not loaded)
    By fengtian.we in forum Qt Programming
    Replies: 4
    Last Post: 9th February 2007, 08:11

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.