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. QSQLITE app no connect to database after deploy
QtWS25 Last Chance

QSQLITE app no connect to database after deploy

Scheduled Pinned Locked Moved Solved General and Desktop
qsqlitedatabasedeploy
13 Posts 4 Posters 1.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.
  • P Offline
    P Offline
    Piotrek102
    wrote on last edited by
    #1

    Hi,
    I have a problem with my application, it performs tasks on a local database located in the application folder. While working in QT and testing in debug and release mode everything works. The problem arises when the application runs outside of QT. By default, I built it in release mode and moved its folder to the desktop. Then using the windeployqt utility I added dll files to it. In the application folder there is the "sqldrivers" folder and in it: qsqlite.dll, qsqlodbc.dll, qsqlpsql.dll. Despite this, the application does not work, that is, it does not connect to the database. In the program I put a function that saves driver errors to a text file. After closing the application, I can read "Driver not loaded".
    Thanks in advance for your help and sorry for my english

    JonBJ 1 Reply Last reply
    0
    • P Piotrek102

      Hi,
      I have a problem with my application, it performs tasks on a local database located in the application folder. While working in QT and testing in debug and release mode everything works. The problem arises when the application runs outside of QT. By default, I built it in release mode and moved its folder to the desktop. Then using the windeployqt utility I added dll files to it. In the application folder there is the "sqldrivers" folder and in it: qsqlite.dll, qsqlodbc.dll, qsqlpsql.dll. Despite this, the application does not work, that is, it does not connect to the database. In the program I put a function that saves driver errors to a text file. After closing the application, I can read "Driver not loaded".
      Thanks in advance for your help and sorry for my english

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Piotrek102
      Set environment variable QT_DEBUG_PLUGINS=1 and then run your application. Look at the end of the diagnostic output.

      1 Reply Last reply
      2
      • P Offline
        P Offline
        Piotrek102
        wrote on last edited by
        #3

        @JonB
        Forgive me, but I'm still a beginner. In which file should I put "QT_DEBUG_PLUGINS = 1"?

        JonBJ 1 Reply Last reply
        0
        • P Piotrek102

          @JonB
          Forgive me, but I'm still a beginner. In which file should I put "QT_DEBUG_PLUGINS = 1"?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Piotrek102

          • If you run your application from a Command Prompt, type set QT_DEBUG_PLUGINS=1 [Enter] and then type the path the path to your executable from there to run it.
          • If you run your application from within Qt Creator, you can set it in the "Run environment variables", or something similar to that.
          1 Reply Last reply
          1
          • P Offline
            P Offline
            Piotrek102
            wrote on last edited by
            #5

            @JonB
            But my app is QT widget application, app is done and work in Qt creator. App work without Qt creator too but no connect to database. I don't run it from the command line but with the exe file from the application folder. I'm sorry if I still don't understand or misinterpreted something but I'm still learning.

            JonBJ 1 Reply Last reply
            0
            • P Piotrek102

              @JonB
              But my app is QT widget application, app is done and work in Qt creator. App work without Qt creator too but no connect to database. I don't run it from the command line but with the exe file from the application folder. I'm sorry if I still don't understand or misinterpreted something but I'm still learning.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Piotrek102 said in QSQLITE app no connect to database after deploy:

              but with the exe file from the application folder

              So follow the first recommendation, running it from a Command Prompt.

              1 Reply Last reply
              1
              • P Offline
                P Offline
                Piotrek102
                wrote on last edited by
                #7

                Ok, I did as you said but the stake starts normally. It also doesn't return any errors. I didn't execute 2 options with QT because the program works fine. I conclude that when I run in QT it has all dll libraries installed. It doesn't work without Qt. When starting the application, I ran off DebugView. I can read the following errors from it:

                • QSqlDatabase: QSQLITE driver not loaded
                • QSqlDatabase: available drivers:
                • QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  Are you by any chance creating a static QSqlDatabase instance somewhere in your application ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • P Offline
                    P Offline
                    Piotrek102
                    wrote on last edited by Piotrek102
                    #9

                    @SGaist
                    I use the database like this:

                    #include <QString>
                    #include <QStringList>
                    #include <QDir>
                    #include <QSql>
                    #include <QSqlDatabase>
                    #include <QSqlQuery>
                    #include <QVariant>
                    #include <QSqlRecord>
                    #include <QFile>
                    #include <QIODevice>
                    #include <QTextStream>
                    
                    #include <QSqlDriver>
                    #include <QSqlError>
                    
                    QSqlDatabase Database = QSqlDatabase::addDatabase("QSQLITE"); 
                    
                    void DataLib::CreateTable(QString table_name, int cols_number, QString cols_names, QString cols_length) 
                    {
                        QString make_command;
                    
                        make_command = "create table XXXtablenameXXX ";
                        make_command.replace("XXXtablenameXXX", table_name);
                    
                        make_command += "(";
                    
                        make_command += "id integer primary key";
                    
                        QStringList col_nam = cols_names.split(";");
                        QStringList col_len = cols_length.split(";");
                        int a = 0;
                        while(a < cols_number)
                        {
                            make_command += ", XXXcolnameXXX varchar(XXXcollengthXXX)";
                            make_command.replace("XXXcolnameXXX",col_nam[a]);
                            make_command.replace("XXXcollengthXXX", col_len[a]);
                            a++; 
                        }
                    
                        make_command += ")";
                    
                        QString data_path = QDir::currentPath() + "/Data/Database.db";
                        Database.setDatabaseName(data_path);
                        if (Database.open())
                        {
                            QSqlQuery query;
                            query.exec(make_command);
                            query.clear();
                        }
                        Database.close();
                    }
                    

                    This is part of my application, I created a function here to simplify my life. I am providing data and the function creates a table in the database for me. So it doesn't use a static database.

                    jsulmJ JonBJ 2 Replies Last reply
                    0
                    • P Piotrek102

                      @SGaist
                      I use the database like this:

                      #include <QString>
                      #include <QStringList>
                      #include <QDir>
                      #include <QSql>
                      #include <QSqlDatabase>
                      #include <QSqlQuery>
                      #include <QVariant>
                      #include <QSqlRecord>
                      #include <QFile>
                      #include <QIODevice>
                      #include <QTextStream>
                      
                      #include <QSqlDriver>
                      #include <QSqlError>
                      
                      QSqlDatabase Database = QSqlDatabase::addDatabase("QSQLITE"); 
                      
                      void DataLib::CreateTable(QString table_name, int cols_number, QString cols_names, QString cols_length) 
                      {
                          QString make_command;
                      
                          make_command = "create table XXXtablenameXXX ";
                          make_command.replace("XXXtablenameXXX", table_name);
                      
                          make_command += "(";
                      
                          make_command += "id integer primary key";
                      
                          QStringList col_nam = cols_names.split(";");
                          QStringList col_len = cols_length.split(";");
                          int a = 0;
                          while(a < cols_number)
                          {
                              make_command += ", XXXcolnameXXX varchar(XXXcollengthXXX)";
                              make_command.replace("XXXcolnameXXX",col_nam[a]);
                              make_command.replace("XXXcollengthXXX", col_len[a]);
                              a++; 
                          }
                      
                          make_command += ")";
                      
                          QString data_path = QDir::currentPath() + "/Data/Database.db";
                          Database.setDatabaseName(data_path);
                          if (Database.open())
                          {
                              QSqlQuery query;
                              query.exec(make_command);
                              query.clear();
                          }
                          Database.close();
                      }
                      

                      This is part of my application, I created a function here to simplify my life. I am providing data and the function creates a table in the database for me. So it doesn't use a static database.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Piotrek102 said in QSQLITE app no connect to database after deploy:

                      QSqlDatabase Database = QSqlDatabase::addDatabase("QSQLITE");

                      Don't do such things! That is exactly what @SGaist was talking about.
                      Read documentation:
                      "Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior."
                      https://doc.qt.io/qt-5/qsqldatabase.html

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

                      1 Reply Last reply
                      3
                      • P Piotrek102

                        @SGaist
                        I use the database like this:

                        #include <QString>
                        #include <QStringList>
                        #include <QDir>
                        #include <QSql>
                        #include <QSqlDatabase>
                        #include <QSqlQuery>
                        #include <QVariant>
                        #include <QSqlRecord>
                        #include <QFile>
                        #include <QIODevice>
                        #include <QTextStream>
                        
                        #include <QSqlDriver>
                        #include <QSqlError>
                        
                        QSqlDatabase Database = QSqlDatabase::addDatabase("QSQLITE"); 
                        
                        void DataLib::CreateTable(QString table_name, int cols_number, QString cols_names, QString cols_length) 
                        {
                            QString make_command;
                        
                            make_command = "create table XXXtablenameXXX ";
                            make_command.replace("XXXtablenameXXX", table_name);
                        
                            make_command += "(";
                        
                            make_command += "id integer primary key";
                        
                            QStringList col_nam = cols_names.split(";");
                            QStringList col_len = cols_length.split(";");
                            int a = 0;
                            while(a < cols_number)
                            {
                                make_command += ", XXXcolnameXXX varchar(XXXcollengthXXX)";
                                make_command.replace("XXXcolnameXXX",col_nam[a]);
                                make_command.replace("XXXcollengthXXX", col_len[a]);
                                a++; 
                            }
                        
                            make_command += ")";
                        
                            QString data_path = QDir::currentPath() + "/Data/Database.db";
                            Database.setDatabaseName(data_path);
                            if (Database.open())
                            {
                                QSqlQuery query;
                                query.exec(make_command);
                                query.clear();
                            }
                            Database.close();
                        }
                        

                        This is part of my application, I created a function here to simplify my life. I am providing data and the function creates a table in the database for me. So it doesn't use a static database.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @Piotrek102
                        As @SGaist & @jsulm have said.

                        To explain behaviour: your situation is worse, as your QSqlDatabase Database is not even a class member variable, it's a global variable (yuck!). This means that line will be executed before your application's main() has even run. At which point you have not created the QApplication yet, hence the error.

                        1 Reply Last reply
                        3
                        • P Offline
                          P Offline
                          Piotrek102
                          wrote on last edited by
                          #12

                          @JonB , @SGaist , @jsulm
                          Thank you very much for your help, as you can see I still have a lot to learn. I will read the documentation again and rewrite the code to make it correct. Thanks again.

                          But what I sent you is not the main application file. It's part of the class I wrote. I am referencing it from the main application file (MainWindow.cpp).

                          To sum up, thank you for your help. I will read it, I will learn it again and I hope it will be successful, if not I know where to look for you ;-)

                          jsulmJ 1 Reply Last reply
                          0
                          • P Piotrek102

                            @JonB , @SGaist , @jsulm
                            Thank you very much for your help, as you can see I still have a lot to learn. I will read the documentation again and rewrite the code to make it correct. Thanks again.

                            But what I sent you is not the main application file. It's part of the class I wrote. I am referencing it from the main application file (MainWindow.cpp).

                            To sum up, thank you for your help. I will read it, I will learn it again and I hope it will be successful, if not I know where to look for you ;-)

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @Piotrek102 said in QSQLITE app no connect to database after deploy:

                            But what I sent you is not the main application file. It's part of the class I wrote

                            It doesn't matter. What @JonB wrote applies.

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

                            1 Reply Last reply
                            3

                            • Login

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