Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved QSQLITE app no connect to database after deploy

    General and Desktop
    qsqlite database deploy
    4
    13
    526
    Loading More Posts
    • 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
      Piotrek102 last edited by

      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

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @Piotrek102 last edited by

        @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 Reply Quote 2
        • P
          Piotrek102 last edited by

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

          JonB 1 Reply Last reply Reply Quote 0
          • JonB
            JonB @Piotrek102 last edited by JonB

            @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 Reply Quote 1
            • P
              Piotrek102 last edited by

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

              JonB 1 Reply Last reply Reply Quote 0
              • JonB
                JonB @Piotrek102 last edited by

                @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 Reply Quote 1
                • P
                  Piotrek102 last edited by

                  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 Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    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 Reply Quote 2
                    • P
                      Piotrek102 last edited by 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.

                      jsulm JonB 2 Replies Last reply Reply Quote 0
                      • jsulm
                        jsulm Lifetime Qt Champion @Piotrek102 last edited by

                        @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 Reply Quote 3
                        • JonB
                          JonB @Piotrek102 last edited by JonB

                          @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 Reply Quote 3
                          • P
                            Piotrek102 last edited by

                            @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 ;-)

                            jsulm 1 Reply Last reply Reply Quote 0
                            • jsulm
                              jsulm Lifetime Qt Champion @Piotrek102 last edited by

                              @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 Reply Quote 3
                              • First post
                                Last post