Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Error: Driver not loaded Driver not loaded on Ubuntu
Forum Updated to NodeBB v4.3 + New Features

[Solved]Error: Driver not loaded Driver not loaded on Ubuntu

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 21.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ebruurs
    wrote on last edited by
    #1

    While running the next to files I'll get this run-time error:
    Database error: Driver not loaded Driver not loaded

    I don't see why this isn't working since i am creating an instance of the db variable in the header and declaring it later on.

    I'm pretty new to Qt and C++ so probably i am just missing something.

    I hope someone can help me with this problem.

    databaseConnector.h
    @#ifndef DATABASECONNECTOR
    #define DATABASECONNECTOR

    #include <QSqlDatabase>

    class databaseConnector
    {
    private:
    QSqlDatabase *db;

    public:
    databaseConnector();
    bool connectDb();
    bool disconnectDb();
    };

    #endif // DATABASECONNECTOR_H@

    databaseConnector.cpp
    @#include "databaseconnector.h"
    #include <QMessageBox>
    #include <QObject>
    #include <stdio.h>
    #include <QSqlError>

    databaseConnector::databaseConnector() {
    db = new QSqlDatabase();
    db->addDatabase("QMYSQL");
    db->setHostName("127.0.0.1");
    db->setDatabaseName("dbname");
    db->setUserName("root");
    db->setPassword("root");
    }

    bool databaseConnector::connectDb() {
    if(!db->open()) {
    QMessageBox::critical(0, QObject::tr("Databse Error"), db->lastError().text());
    return false;
    }
    return true;
    }

    bool databaseConnector::disconnectDb() {
    return true;
    }@

    main.cpp
    @
    #include <QtGui>
    #include <QApplication>
    #include <stdio.h>
    #include #databaseconnector.h"

    int main(int argc, char *argv[])
    {
    databaseConnector *dbconnector = new databaseConnector();

    if(dbconnector->connectDb()){
    printf("Connection failed");
    }
    printf("Connection worked");
    }@

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dialingo
      wrote on last edited by
      #2

      Did you install a mysql driver for Qt? On Ubuntu you need to install libqt4-sql-mysql

      1 Reply Last reply
      0
      • E Offline
        E Offline
        ebruurs
        wrote on last edited by
        #3

        I did install the mysql driver so that shouldn't be the problem. Thanks again for any help on this.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          Your code compiles, so there is no real error in that.

          It's very likely that the mysql plugin for Qt is not found.

          Some points to start reading:

          • General instructions on "Deploying Qt Applications":http://doc.qt.nokia.com/4.7/deployment.html
          • "Deploying an Application on X11 Platforms ":http://doc.trolltech.com/4.7/deployment-x11.html

          If change your main method to the following, what does the console output say?

          @
          #include <QtGui>
          #include <QApplication>
          #include <QDebug>
          #include <stdio.h>
          #include "databaseconnector.h"
           
          int main(int argc, char *argv[])
          {
          QApplication a(argc, argv);

          qDebug() << qApp->libraryPaths();

          databaseConnector *dbconnector = new databaseConnector();
           
          if(dbconnector->connectDb()){
             printf("Connection failed");
            }

          printf("Connection worked");
          }
          @

          Is the mysql plugin listed in a subdir "sqldrivers" in one of the paths printed?

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • E Offline
            E Offline
            ebruurs
            wrote on last edited by
            #5

            Thank you for your reply.

            the code does compile so I know that it isn't a wrong syntax. But it gives the error during runtime.

            When I change the code in main(), and follow the paths, I have a folder sqldrivers in one of those paths. In that folder there is a file called libqsqlmysql.so. I suppose that the mysql driver is installed and works correctly.

            The thing is that when I change only a small part of my code it works, but then I can't declare the variable db global (in the header file).
            The things i changed:

            • remove the private: part in the header file
            • Place the next code in the function databaseConnector::connectDb()
              @
              QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
              db.setHostName("127.0.0.1");
              db.setDatabaseName("Nedtrain");
              db.setUserName("root");
              db.setPassword("root");
              if(!db.open()) {
              QMessageBox::critical(0, QObject::tr("Databse Error"), db.lastError().text());
              return false;
              }
              return true;
              @
              and clear all the code in the constructor.

            Thanks again for any reply's

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              Ok, that's clear. The db object is instantiated at the wrong time (no library paths setup, for example) and so fails to load the plugin.

              You should try hard to avoid global variables.

              Oh, and you can retrieve the db object any time later, no need to save it in a global variable.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • E Offline
                E Offline
                ebruurs
                wrote on last edited by
                #7

                So correct me if I am wrong;

                Because I use a QSqlDatabase db in the header, the paths aren't instantiated yet. So I can't use the db object later on. So when I want to close my database in an new function I can't just use the db object declared in the header (because I get the error then), or can I? Or should I use the next code part to close the database?
                @
                void databaseConnector::disconnectDb() {
                QSqlDatabase db = QSqlDatabase::database();
                db.close();
                db.removeDatabase("QMYSQL");
                }
                @

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  If you declare the variable in the header as global, it is up to the compiler, when exactly the object is created (and that varies from compiler to compiler!), so it can be that there is an attempt to create the object at a time, when Qt is not finally set up yet - which can lead to not finding plugins and the like.

                  The steps are basically:

                  1. create a database connection somewhere in a connectDb method or the like:

                  @
                  QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
                  db.setHostName("acidalia");
                  db.setDatabaseName("customdb");
                  db.setUserName("mojito");
                  db.setPassword("J0a1m8");
                  bool ok = db.open();
                  @
                  The above snippet creates a default database; if you need more DBs, add a second parameter to the addDatabase call with the internal name you want to identify it with.

                  1. get the database handle wherever you need it

                  @
                  QSqlDatabase db = QSqlDatabase::database();
                  @
                  This retrieves the previously setup default database; add a name to it if you have multiple ones.

                  1. close the database in a disconnectDB method
                    Get the handle like above and call close on it.

                  This way you get rid of the global object and hence avoid initialization hassles caused by the linker.

                  Oh, and an important note:
                  "QSqlDatabase::addDatabase() ":http://doc.qt.nokia.com/4.7/qsqldatabase.html#addDatabase, you must not call it on an QSqlDatabase object created with new! Just do it like the samples in the docs (= the snippets I copied above) show you; you just need to change the driver name, username, password, etc.

                  [Code snippets copied from the "QSqlDatabase documentation":http://doc.qt.nokia.com/4.7/qsqldatabase.html of Qt]

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    ebruurs
                    wrote on last edited by
                    #9

                    Thank you for your reply, Think I understand now what went wrong in the first place. Also got it working now at the method you descriped.

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      redkite
                      wrote on last edited by
                      #10

                      [quote author="Volker" date="1304607729"]@ ...
                      qDebug() << qApp->libraryPaths();
                      ...@

                      Is the mysql plugin listed in a subdir "sqldrivers" in one of the paths printed?[/quote]

                      Thanks for those accurate advices Volker, I was wondering why the driver wasn't found when I past the .so next to my executable. I was missing to put it in a subfolder named sqldrivers !

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved