Results 1 to 3 of 3

Thread: QSqlDatabase as protected member results in "Driver not loaded"

  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 as protected member results in "Driver not loaded"

    Hi.

    I'm trying to implement a generic connection class using Qt for my projects.

    The code looks like this:

    connection.h
    Qt Code:
    1. #ifndef CONNECTION_H
    2. #define CONNECTION_H
    3.  
    4. #include <QtSql/QSqlDatabase>
    5.  
    6. class Connection
    7. {
    8. public:
    9. bool open();
    10. void close();
    11. bool isOpen() const;
    12.  
    13. QString databaseName() const;
    14. QString hostName() const;
    15. QString userName() const;
    16. QString password() const;
    17. int port() const;
    18.  
    19. QString lastError() const;
    20.  
    21. operator QSqlDatabase();
    22. operator QSqlDatabase*();
    23.  
    24. protected:
    25. void setDatabaseName(const QString &databaseName);
    26. void setHostName(const QString &hostName);
    27. void setUserName(const QString &userName);
    28. void setPassword(const QString &password);
    29. void setPort(int port);
    30.  
    31. protected:
    32. };
    33.  
    34. #endif // CONNECTION_H
    To copy to clipboard, switch view to plain text mode 

    connection.cpp
    Qt Code:
    1. #include <QtSql/QSqlError>
    2.  
    3. #include "connection.h"
    4.  
    5. bool Connection::open()
    6. {
    7. return m_db.open();
    8. }
    9.  
    10. void Connection::close()
    11. {
    12. if (isOpen())
    13. m_db.close();
    14. }
    15.  
    16. bool Connection::isOpen() const
    17. {
    18. return m_db.isOpen();
    19. }
    20.  
    21. QString Connection::databaseName() const
    22. {
    23. return m_db.databaseName();
    24. }
    25.  
    26. QString Connection::hostName() const
    27. {
    28. return m_db.hostName();
    29. }
    30.  
    31. QString Connection::userName() const
    32. {
    33. return m_db.userName();
    34. }
    35.  
    36. QString Connection::password() const
    37. {
    38. return m_db.password();
    39. }
    40.  
    41. int Connection::port() const
    42. {
    43. return m_db.port();
    44. }
    45.  
    46. QString Connection::lastError() const
    47. {
    48. return m_db.lastError().databaseText();
    49. }
    50.  
    51. Connection::operator QSqlDatabase()
    52. {
    53. return m_db;
    54. }
    55.  
    56. Connection::operator QSqlDatabase *()
    57. {
    58. return &m_db;
    59. }
    60.  
    61. void Connection::setDatabaseName(const QString &databaseName)
    62. {
    63. if (isOpen())
    64. close();
    65.  
    66. if (m_db.databaseName() != databaseName)
    67. m_db.setDatabaseName(databaseName);
    68. }
    69.  
    70. void Connection::setHostName(const QString &hostName)
    71. {
    72. if (isOpen())
    73. close();
    74.  
    75. m_db.setHostName(hostName);
    76. }
    77.  
    78. void Connection::setUserName(const QString &userName)
    79. {
    80. if (isOpen())
    81. close();
    82.  
    83. m_db.setUserName(userName);
    84. }
    85.  
    86. void Connection::setPassword(const QString &password)
    87. {
    88. if (isOpen())
    89. close();
    90.  
    91. m_db.setPassword(password);
    92. }
    93.  
    94. void Connection::setPort(int port)
    95. {
    96. if (isOpen())
    97. close();
    98.  
    99. m_db.setPort(port);
    100. }
    To copy to clipboard, switch view to plain text mode 

    sqliteconnection.h
    Qt Code:
    1. #ifndef SQLITECONNECTION_H
    2. #define SQLITECONNECTION_H
    3.  
    4. class Connection;
    5.  
    6. class SQLiteConnection : public Connection
    7. {
    8. public:
    9. SQLiteConnection();
    10. SQLiteConnection(const QString &databaseName);
    11.  
    12. bool open(const QString &databaseName);
    13. };
    14.  
    15. #endif // SQLITECONNECTION_H
    To copy to clipboard, switch view to plain text mode 

    sqliteconnection.cpp
    Qt Code:
    1. #include <QtCore/QUuid>
    2.  
    3. #include "connection.h"
    4. #include "sqliteconnection.h"
    5.  
    6. SQLiteConnection::SQLiteConnection()
    7. {
    8. m_db = QSqlDatabase::addDatabase("QSQLITE", QUuid::createUuid().toString());
    9. }
    10.  
    11. SQLiteConnection::SQLiteConnection(const QString &databaseName)
    12. {
    13. SQLiteConnection();
    14.  
    15. Connection::setDatabaseName(databaseName);
    16. }
    17.  
    18. bool SQLiteConnection::open(const QString &databaseName)
    19. {
    20. Connection::setDatabaseName(databaseName);
    21.  
    22. return Connection::open();
    23. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtWidgets/QApplication>
    2.  
    3. #include "monetae.h"
    4.  
    5. #include "db/connection.h"
    6. #include "db/sqliteconnection.h"
    7. #include "db/mysqlconnection.h"
    8.  
    9. #include <QtSql/QSqlQuery>
    10. #include <QtCore/QDebug>
    11.  
    12. int main(int argc, char *argv[])
    13. {
    14. QApplication application(argc, argv);
    15.  
    16. Connection con = SQLiteConnection("~/test.sq3");
    17.  
    18. if (con.open() == false) {
    19. qDebug() << con.lastError();
    20.  
    21. return -1;
    22. }
    23.  
    24. QSqlQuery qry(con);
    25.  
    26. qry.exec("CREATE TABLE TEST (ID INTEGER, NAME VARCHAR(10))");
    27.  
    28. return 0;
    To copy to clipboard, switch view to plain text mode 

    The problem is that con.open() fails and con.lastError() returns "Driver not loaded". But if m_db is a private member in SqliteConnection and not a protected member in Connection everything works fine.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase as protected member results in "Driver not loaded"

    My guess is that you are not initializing m_db in the constructor you are using, only in the SQLiteConnection's default constructor.

    I would recommend something like this so that this won't happen

    Qt Code:
    1. class Connection
    2. {
    3. protected:
    4. Connection(const QString &driver, const QString &dbName)
    5. m_db(QSqlDatabase::addDatabase(driver, dbName))
    6. {
    7. }
    8. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. SQLiteConnection::SQLiteConnection()
    2. : Connection("QSQLITE", QUuid::createUuid().toString())
    3. {
    4. }
    5.  
    6. SQLiteConnection::SQLiteConnection(const QString &databaseName)
    7. : Connection("QSQLITE", databaseName)
    8. {
    9. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

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

    croscato (6th February 2014)

  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 as protected member results in "Driver not loaded"

    Many thanks anda_skoa!

    You sugestion work like a charm.

Similar Threads

  1. Android / necessitas: SQLITE error ("Driver not loaded")
    By Al_ in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 28th August 2011, 16:06
  2. App found qsqlite4.dll driver file but still "Driver not loaded"
    By merelendor in forum Installation and Deployment
    Replies: 2
    Last Post: 17th August 2011, 12:15
  3. Replies: 1
    Last Post: 24th April 2010, 04:43
  4. Replies: 4
    Last Post: 10th November 2009, 20:48
  5. The strange problem:"QSqlDatabase: QMYSQL driver not loaded"
    By osmanthus in forum Installation and Deployment
    Replies: 2
    Last Post: 22nd January 2009, 16:12

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.