QSQLITE app no connect to database after deploy
-
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 -
@Piotrek102
Set environment variableQT_DEBUG_PLUGINS=1
and then run your application. Look at the end of the diagnostic output. -
@JonB
Forgive me, but I'm still a beginner. In which file should I put "QT_DEBUG_PLUGINS = 1"? -
- 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.
- If you run your application from a Command Prompt, type
-
@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. -
@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.
-
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
-
Hi,
Are you by any chance creating a static QSqlDatabase instance somewhere in your application ?
-
@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.
-
@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 -
@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'smain()
has even run. At which point you have not created theQApplication
yet, hence the error. -
@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 ;-)
-
@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.