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. Getting QSqlError "Driver not loaded" when trying to use QSQLITE database
Forum Updated to NodeBB v4.3 + New Features

Getting QSqlError "Driver not loaded" when trying to use QSQLITE database

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 11.3k Views
  • 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.
  • S Offline
    S Offline
    stewpid
    wrote on last edited by
    #1

    Hi all, this is my first post for asking for anything programming related, so please forgive me if I'm missing something.

    I'm doing a project for school that requires CRUD functionality; however, none of the databases are working for QT.

    Here's what I'm doing to connect to the database:
    ```

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "SQLITE");
        QString dbName = "path/database.sqlite";
        db.setDatabaseName(dbName);
        qDebug() << db.connectionName();
    
    
        if (!db.open())
        {
            qDebug() << "There was an error opening the file";
        }
        else
        {
           qDebug() << QSqlDatabase::drivers();
           qDebug() << "Database opened successfully!";
    
           QSqlQuery q;
           q.exec("select * from login");
           while(q.next())
            {
                int id = q.value(0).toInt();
                qDebug() << id; /*testing to see if the console will return any of the entry_ids from the file*/
            }
    

    When doing this, I get the error :

    QSqlQuery::exec: database not open
    There was an issue opening the database
    QSqlError("", "Driver not loaded", "Driver not loaded")
    

    Can someone help with troubleshooting the drivers? I've tried the steps in the QT SQLITE DRIVER setup, but do not have the nmake command available for use in terminal. Read more here: https://doc.qt.io/qt-5/sql-driver.html#qsqlite.

    SPECS:
    Qt Creator 4.13.3
    Based on Qt 5.15.2 (Clang 11.0 (Apple), 64 bit)
    Built on Nov 13 2020 12:39:07
    MAC OSX 11.0.1 Beta (20B5022a)

    1 Reply Last reply
    0
    • S stewpid

      @jsulm thanks for your recommendation. I've looked up and down the documentation for this; however, I'm not sure how to do this.

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #9

      @stewpid

      Just add the variable to Creators Run settings.

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      3
      • B Offline
        B Offline
        Bonnie
        wrote on last edited by Bonnie
        #2

        Qt has sqlite plugin installed by default, you don't need to build your own.
        And your db is successfully opened, but since you call addDatabase with a specified connectionName, so it is not the "default connection".
        When there is no "default connection", it is required to specified the db when constructing QSqlQuery.
        So there're two solutions:

        1. Specify the db when constructing QSqlQuery
        QSqlQuery q(db);
        
        1. Or make your db the "default connection" by leaving the connectionName unspecified. This is what I would prefer if you have only one db to manage.
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        
        S 1 Reply Last reply
        3
        • S Offline
          S Offline
          stewpid
          wrote on last edited by
          #3

          Hi Bonnie,

          Thank you so much for your response. I tried what you stated and omitted the connection name, but I'm still getting a driver issue when testing to see if the database is open later.

          Right after opening the connection, I test to see if the database is open:

           if (!db.open())
              {
                  qDebug() << "There was an error opening the file";
              }
              else
              {
                 qDebug() << QSqlDatabase::drivers();
                 qDebug() << "Database opened successfully!";
          
                 QSqlQuery q;
                 q.exec("select * from login");
                 while(q.next())
                  {
                      int id = q.value(0).toInt();
                      qDebug() << id; /*testing to see if the console will return any of the entry_ids from the file*/
                  }
          

          However, I get this from db.lastError();

          QSqlError("", "Driver not loaded", "Driver not loaded")
          ("QSQLITE", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
          

          This shouldn't be the case, right? It says that I have the drivers for SQLite available, but they're not loading for some reason...

          Thanks in advance!

          jsulmJ B 2 Replies Last reply
          0
          • S stewpid

            Hi Bonnie,

            Thank you so much for your response. I tried what you stated and omitted the connection name, but I'm still getting a driver issue when testing to see if the database is open later.

            Right after opening the connection, I test to see if the database is open:

             if (!db.open())
                {
                    qDebug() << "There was an error opening the file";
                }
                else
                {
                   qDebug() << QSqlDatabase::drivers();
                   qDebug() << "Database opened successfully!";
            
                   QSqlQuery q;
                   q.exec("select * from login");
                   while(q.next())
                    {
                        int id = q.value(0).toInt();
                        qDebug() << id; /*testing to see if the console will return any of the entry_ids from the file*/
                    }
            

            However, I get this from db.lastError();

            QSqlError("", "Driver not loaded", "Driver not loaded")
            ("QSQLITE", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
            

            This shouldn't be the case, right? It says that I have the drivers for SQLite available, but they're not loading for some reason...

            Thanks in advance!

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @stewpid Set QT_DEBUG_PLUGINS before starting your app and check its output (https://doc.qt.io/qt-5/debug.html#environment-variables-recognized-by-qt).

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            S 1 Reply Last reply
            2
            • S stewpid

              Hi Bonnie,

              Thank you so much for your response. I tried what you stated and omitted the connection name, but I'm still getting a driver issue when testing to see if the database is open later.

              Right after opening the connection, I test to see if the database is open:

               if (!db.open())
                  {
                      qDebug() << "There was an error opening the file";
                  }
                  else
                  {
                     qDebug() << QSqlDatabase::drivers();
                     qDebug() << "Database opened successfully!";
              
                     QSqlQuery q;
                     q.exec("select * from login");
                     while(q.next())
                      {
                          int id = q.value(0).toInt();
                          qDebug() << id; /*testing to see if the console will return any of the entry_ids from the file*/
                      }
              

              However, I get this from db.lastError();

              QSqlError("", "Driver not loaded", "Driver not loaded")
              ("QSQLITE", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
              

              This shouldn't be the case, right? It says that I have the drivers for SQLite available, but they're not loading for some reason...

              Thanks in advance!

              B Offline
              B Offline
              Bonnie
              wrote on last edited by Bonnie
              #5

              @stewpid
              I can't tell since you haven't shown your full code and full output.
              What's the return value of db.open() in your above code? Where do you call db.lastError()?
              My guess is still: you might be operating on an invalid QSqlDatabase object.

              S 1 Reply Last reply
              1
              • B Bonnie

                Qt has sqlite plugin installed by default, you don't need to build your own.
                And your db is successfully opened, but since you call addDatabase with a specified connectionName, so it is not the "default connection".
                When there is no "default connection", it is required to specified the db when constructing QSqlQuery.
                So there're two solutions:

                1. Specify the db when constructing QSqlQuery
                QSqlQuery q(db);
                
                1. Or make your db the "default connection" by leaving the connectionName unspecified. This is what I would prefer if you have only one db to manage.
                QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                
                S Offline
                S Offline
                stewpid
                wrote on last edited by
                #6

                @Bonnie Hi Bonnie,

                Thank you so much for your response. I tried what you stated and omitted the connection name, but I'm still getting a driver issue when testing to see if the database is open later.

                Right after opening the connection, I test to see if the database is open:

                if (!db.open())
                   {
                       qDebug() << "There was an error opening the file";
                   }
                   else
                   {
                      qDebug() << QSqlDatabase::drivers();
                      qDebug() << "Database opened successfully!";
                
                      QSqlQuery q;
                      q.exec("select * from login");
                      while(q.next())
                       {
                           int id = q.value(0).toInt();
                           qDebug() << id; /*testing to see if the console will return any of the entry_ids from the file*/
                       }
                

                However, I get this from db.lastError();

                QSqlError("", "Driver not loaded", "Driver not loaded")
                ("QSQLITE", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
                This shouldn't be the case, right? It says that I have the drivers for SQLite available, but they're not loading for some reason...

                Thanks in advance!

                JonBJ 1 Reply Last reply
                0
                • S stewpid

                  @Bonnie Hi Bonnie,

                  Thank you so much for your response. I tried what you stated and omitted the connection name, but I'm still getting a driver issue when testing to see if the database is open later.

                  Right after opening the connection, I test to see if the database is open:

                  if (!db.open())
                     {
                         qDebug() << "There was an error opening the file";
                     }
                     else
                     {
                        qDebug() << QSqlDatabase::drivers();
                        qDebug() << "Database opened successfully!";
                  
                        QSqlQuery q;
                        q.exec("select * from login");
                        while(q.next())
                         {
                             int id = q.value(0).toInt();
                             qDebug() << id; /*testing to see if the console will return any of the entry_ids from the file*/
                         }
                  

                  However, I get this from db.lastError();

                  QSqlError("", "Driver not loaded", "Driver not loaded")
                  ("QSQLITE", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
                  This shouldn't be the case, right? It says that I have the drivers for SQLite available, but they're not loading for some reason...

                  Thanks in advance!

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #7

                  @stewpid
                  You should be following @jsulm's instruction:

                  @stewpid Set QT_DEBUG_PLUGINS before starting your app and check its output (https://doc.qt.io/qt-5/debug.html#environment-variables-recognized-by-qt).

                  1 Reply Last reply
                  2
                  • jsulmJ jsulm

                    @stewpid Set QT_DEBUG_PLUGINS before starting your app and check its output (https://doc.qt.io/qt-5/debug.html#environment-variables-recognized-by-qt).

                    S Offline
                    S Offline
                    stewpid
                    wrote on last edited by
                    #8

                    @jsulm thanks for your recommendation. I've looked up and down the documentation for this; however, I'm not sure how to do this.

                    aha_1980A 1 Reply Last reply
                    0
                    • S stewpid

                      @jsulm thanks for your recommendation. I've looked up and down the documentation for this; however, I'm not sure how to do this.

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      @stewpid

                      Just add the variable to Creators Run settings.

                      Regards

                      Qt has to stay free or it will die.

                      1 Reply Last reply
                      3
                      • B Bonnie

                        @stewpid
                        I can't tell since you haven't shown your full code and full output.
                        What's the return value of db.open() in your above code? Where do you call db.lastError()?
                        My guess is still: you might be operating on an invalid QSqlDatabase object.

                        S Offline
                        S Offline
                        stewpid
                        wrote on last edited by
                        #10

                        @Bonnie Hi Bonnie, I think your solution is correct! Thank you so much

                        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