How to connect to the database correctly in QtWebApp
-
Good afternoon.
I do it on the QtWebApp server. Made the DataBase class to work with the database.
If I open a connection to the base in the RequestHandler class in the constructor, the treatment to the base in the functionvoid RequestHandler::service(HttpRequest& request, HttpResponse& response)
a connection error is returned, and if I open a connection in this function, it works normally for one request, if there is 2 requests, while the first request is wooling the database, then an error is issued and the server crashes.
07.06.2020 15:36:44.134 1 WARNING 0x20d4 QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
Please tell me how and where to create an object of the DataBase class and open it so that everything works?
-
Hi,
If you open a new connection for each request then you have to give them unique names and not forget to delete said connection at the end of the request handling.
-
@Mikeeeeee said in How to connect to the database correctly in QtWebApp:
What do you mean by unique names?
-
What exactly did you do ?
What did the stack trace of the crash tell you ? -
Each new object of the DataBase class reconnects to the database. This object is created each time in a function
void RequestHandler::service(HttpRequest& request, HttpResponse& response)
And I get error:
07.06.2020 15:36:44.134 1 WARNING 0x20d4 QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
-
@Mikeeeeee said in How to connect to the database correctly in QtWebApp:
duplicate connection name 'qt_sql_default_connection'
It doesn't look like you really read the documentation: you do NOT set database name!
Also it would be helpful if you would post your code... -
It is my code fore open database:
void DataBase::openDB() { QTextStream out(stdout); ///* dataBase = QSqlDatabase::addDatabase("QPSQL"); dataBase.setDatabaseName("db_game_tamada"); dataBase.setUserName("postgres"); dataBase.setPassword("1qaz"); dataBase.setPort(5433);//*/ if (dataBase.open()) { //out<<"db is opened \r\n"; } else { //out<<"db not opened \r\n"; } } void DataBase::closeDB() { dataBase.close(); }
I tried to use it like this:
void RequestHandler::service(HttpRequest& request, HttpResponse& response) { DataBase db; db.openDB();
-
As we already suggested several times: go read the documentation of addDatabase. You are not setting a connection name, therefore all your DataBase objects are using default connection.
And as I already have written numerous times: Do not keep a local QSqlDatabase member variable. There's a warning in the documentation about that.
-
How I can set connection name? I try this, but this code not work
dataBase = QSqlDatabase::addDatabase("QPSQL", "conectionName"); dataBase.setDatabaseName("db_game_tamada"); dataBase.setUserName("postgres"); dataBase.setPassword("1qaz"); dataBase.setPort(5433);//*/ if (dataBase.open()) { //out<<"db is opened \r\n"; } else { //out<<"db not opened \r\n"; }
I get error: driver not loaded
-
You need to use a unique name for each connection.
As for the error, there's usually a bit more information than that and if not, then set the
QT_DEBUG_PLUGINS
environment variable to 1 to get more output.You can do it in the Run part of the Project panel in Qt Creator.
-
You used the way the documentation explains.
You have now what seems to be an unrelated error.
You were asked to provide additional information.
Provide them so we can find what is going on. -
@SGaist said in How to connect to the database correctly in QtWebApp:
QT_DEBUG_PLUGINS
I use windows7 and in cmd I write "QT_DEBUG_PLUGINS=1" and get error: QT_DEBUG_PLUGINS is not an internal or external command.
How use QT_DEBUG_PLUGINS in windows? -
@Mikeeeeee
We have previously told you: under Windows in a Command Prompt you have to write:set QT_DEBUG_PLUGINS=1
If you are developer it's a really basic thing, you should take your own time to read how to set an environment variable, it's not a Qt thing.
-
I get this in server log:
08.06.2020 13:53:10.942 0 DEBUG 0x26c0 ServiceHelper: Starting service 08.06.2020 13:53:11.042 0 DEBUG 0x26c0 HttpListener: Listening on port 8080 08.06.2020 13:53:11.042 1 WARNING 0x26c0 Startup: Service has started 08.06.2020 13:53:19.333 0 DEBUG 0x26c0 HttpListener: New connection 08.06.2020 13:53:19.333 0 DEBUG 0x26c0 HttpConnectionHandler (0x112c0a0): thread started 08.06.2020 13:53:19.333 0 DEBUG 0x26c0 HttpConnectionHandler (0x112c0a0): constructed 08.06.2020 13:53:19.333 0 DEBUG 0x2838 HttpConnectionHandler (0x112c0a0): handle new connection 08.06.2020 13:53:19.334 0 DEBUG 0x2838 HttpConnectionHandler (0x112c0a0): read input 08.06.2020 13:53:19.334 0 DEBUG 0x2838 HttpRequest: read request 08.06.2020 13:53:19.334 0 DEBUG 0x2838 HttpRequest: from ::ffff:127.0.0.1: POST /getAllCategory HTTP/1.1 08.06.2020 13:53:19.334 0 DEBUG 0x2838 HttpRequest: received header host: 127.0.0.1:8080 08.06.2020 13:53:19.334 0 DEBUG 0x2838 HttpRequest: received header content-type: application/json 08.06.2020 13:53:19.334 0 DEBUG 0x2838 HttpRequest: received header content-length: 46 08.06.2020 13:53:19.334 0 DEBUG 0x2838 HttpRequest: received header connection: Keep-Alive 08.06.2020 13:53:19.334 0 DEBUG 0x2838 HttpRequest: received header accept-encoding: gzip, deflate 08.06.2020 13:53:19.334 0 DEBUG 0x2838 HttpRequest: received header accept-language: ru-RU,en,* 08.06.2020 13:53:19.334 0 DEBUG 0x2838 HttpRequest: received header user-agent: Mozilla/5.0 08.06.2020 13:53:19.335 0 DEBUG 0x2838 HttpRequest: headers completed 08.06.2020 13:53:19.335 0 DEBUG 0x2838 HttpRequest: expect 46 bytes body 08.06.2020 13:53:19.443 0 DEBUG 0x2838 HttpConnectionHandler (0x112c0a0): read input 08.06.2020 13:53:19.443 0 DEBUG 0x2838 HttpRequest: receive body 08.06.2020 13:53:19.443 0 DEBUG 0x2838 HttpRequest: extract and decode request parameters 08.06.2020 13:53:19.443 0 DEBUG 0x2838 HttpRequest: extract cookies 08.06.2020 13:53:19.443 0 DEBUG 0x2838 HttpConnectionHandler (0x112c0a0): received request 08.06.2020 13:53:19.444 0 DEBUG 0x2838 QFactoryLoader::QFactoryLoader() checking directory path "E:/Programs/Qt/5.15.0/mingw81_64/plugins/sqldrivers" ... 08.06.2020 13:53:19.445 0 DEBUG 0x2838 QFactoryLoader::QFactoryLoader() looking at "E:/Programs/Qt/5.15.0/mingw81_64/plugins/sqldrivers/qsqlite.dll" 08.06.2020 13:53:19.447 1 WARNING 0x2838 Found metadata in lib E:/Programs/Qt/5.15.0/mingw81_64/plugins/sqldrivers/qsqlite.dll, metadata= { "IID": "org.qt-project.Qt.QSqlDriverFactoryInterface", "MetaData": { "Keys": [ "QSQLITE" ] }, "archreq": 0, "className": "QSQLiteDriverPlugin", "debug": false, "version": 331520 } 08.06.2020 13:53:19.447 0 DEBUG 0x2838 Got keys from plugin meta data ("QSQLITE") 08.06.2020 13:53:19.447 0 DEBUG 0x2838 QFactoryLoader::QFactoryLoader() looking at "E:/Programs/Qt/5.15.0/mingw81_64/plugins/sqldrivers/qsqlodbc.dll" 08.06.2020 13:53:19.448 1 WARNING 0x2838 Found metadata in lib E:/Programs/Qt/5.15.0/mingw81_64/plugins/sqldrivers/qsqlodbc.dll, metadata= { "IID": "org.qt-project.Qt.QSqlDriverFactoryInterface", "MetaData": { "Keys": [ "QODBC3", "QODBC" ] }, "archreq": 0, "className": "QODBCDriverPlugin", "debug": false, "version": 331520 } 08.06.2020 13:53:19.448 0 DEBUG 0x2838 Got keys from plugin meta data ("QODBC3", "QODBC") 08.06.2020 13:53:19.448 0 DEBUG 0x2838 QFactoryLoader::QFactoryLoader() looking at "E:/Programs/Qt/5.15.0/mingw81_64/plugins/sqldrivers/qsqlpsql.dll" 08.06.2020 13:53:19.449 1 WARNING 0x2838 Found metadata in lib E:/Programs/Qt/5.15.0/mingw81_64/plugins/sqldrivers/qsqlpsql.dll, metadata= { "IID": "org.qt-project.Qt.QSqlDriverFactoryInterface", "MetaData": { "Keys": [ "QPSQL7", "QPSQL" ] }, "archreq": 0, "className": "QPSQLDriverPlugin", "debug": false, "version": 331520 } 08.06.2020 13:53:19.449 0 DEBUG 0x2838 Got keys from plugin meta data ("QPSQL7", "QPSQL") 08.06.2020 13:53:19.449 0 DEBUG 0x2838 QFactoryLoader::QFactoryLoader() checking directory path "E:/QTProject/build-HttpSerwerQtWebAppGameTamada-Desktop_Qt_5_15_0_MinGW_64_bit-Debug/debug/sqldrivers" ... 08.06.2020 13:53:19.457 0 DEBUG 0x2838 loaded library "E:/Programs/Qt/5.15.0/mingw81_64/plugins/sqldrivers/qsqlpsql.dll" 08.06.2020 13:53:19.504 1 WARNING 0x2838 QSqlQuery::prepare: database not open 08.06.2020 13:53:19.504 0 DEBUG 0x2838 Conroller: finished request 08.06.2020 13:53:19.504 0 DEBUG 0x2838 HttpConnectionHandler (0x112c0a0): finished request
-
@Mikeeeeee said in How to connect to the database correctly in QtWebApp:
08.06.2020 13:53:19.504 1 WARNING 0x2838 QSqlQuery::prepare: database not open
-
Rather than doing nothing in your if code that checks whether the database is opened, what about printing the error you get form QSqlDatabase ?