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

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

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 10.7k 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 1 Dec 2020, 21:38 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
      2 Dec 2020, 08:21

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

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 2 Dec 2020, 08:22 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 1 Dec 2020, 23:34 last edited by Bonnie 12 Jan 2020, 23:56
        #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 2 Dec 2020, 08:07
        3
        • S Offline
          S Offline
          stewpid
          wrote on 2 Dec 2020, 07:29 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!

          J B 2 Replies Last reply 2 Dec 2020, 07:30
          0
          • S stewpid
            2 Dec 2020, 07:29

            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!

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 2 Dec 2020, 07:30 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 Dec 2020, 08:21
            2
            • S stewpid
              2 Dec 2020, 07:29

              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 2 Dec 2020, 07:56 last edited by Bonnie 12 Feb 2020, 07:58
              #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 2 Dec 2020, 21:31
              1
              • B Bonnie
                1 Dec 2020, 23:34

                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 2 Dec 2020, 08:07 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!

                J 1 Reply Last reply 2 Dec 2020, 08:15
                0
                • S stewpid
                  2 Dec 2020, 08:07

                  @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!

                  J Online
                  J Online
                  JonB
                  wrote on 2 Dec 2020, 08:15 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
                  • J jsulm
                    2 Dec 2020, 07:30

                    @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 2 Dec 2020, 08:21 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.

                    A 1 Reply Last reply 2 Dec 2020, 08:22
                    0
                    • S stewpid
                      2 Dec 2020, 08:21

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

                      A Offline
                      A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on 2 Dec 2020, 08:22 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
                        2 Dec 2020, 07:56

                        @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 2 Dec 2020, 21:31 last edited by
                        #10

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

                        1 Reply Last reply
                        0

                        8/10

                        2 Dec 2020, 08:21

                        • Login

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