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. How to connect to the database correctly in QtWebApp
Forum Updated to NodeBB v4.3 + New Features

How to connect to the database correctly in QtWebApp

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 5 Posters 2.6k Views 1 Watching
  • 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.
  • M Mikeeeeee

    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.
    
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #8

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

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

    1 Reply Last reply
    1
    • M Offline
      M Offline
      Mikeeeeee
      wrote on last edited by
      #9

      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();
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #10

        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.

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

        SGaistS 1 Reply Last reply
        1
        • M Offline
          M Offline
          Mikeeeeee
          wrote on last edited by
          #11

          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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #12

            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.

            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
            1
            • M Offline
              M Offline
              Mikeeeeee
              wrote on last edited by
              #13

              But how to set the name of the database connection? My previous version does not work even with 1 connection. Please write a correct example.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #14

                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.

                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
                1
                • M Offline
                  M Offline
                  Mikeeeeee
                  wrote on last edited by
                  #15

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

                  JonBJ 1 Reply Last reply
                  0
                  • M Mikeeeeee

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

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

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

                    1 Reply Last reply
                    1
                    • M Offline
                      M Offline
                      Mikeeeeee
                      wrote on last edited by
                      #17

                      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
                      
                      JonBJ 1 Reply Last reply
                      0
                      • M Mikeeeeee

                        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
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #18

                        @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

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          Mikeeeeee
                          wrote on last edited by Mikeeeeee
                          #19

                          Yes, if I set connect name, then database not opened. How I can set correctly connect name?

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #20

                            Rather than doing nothing in your if code that checks whether the database is opened, what about printing the error you get form QSqlDatabase ?

                            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
                            1
                            • M Offline
                              M Offline
                              Mikeeeeee
                              wrote on last edited by
                              #21

                              Sanks, it is work

                                  dataBase = QSqlDatabase::addDatabase("QPSQL", "conectionName");
                                  dataBase.setDatabaseName("db_game_tamada");
                                  dataBase.setUserName("postgres");
                                  dataBase.setPassword("1qaz");
                                  //ResumeDB.setHostName("127.0.0.1");
                                  dataBase.setPort(5433);//*/
                              

                              and in query need set database

                              QSqlQuery query(dataBase);
                              
                              1 Reply Last reply
                              0
                              • SGaistS SGaist

                                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.

                                SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #22

                                Repeating myself:

                                @SGaist said in How to connect to the database correctly in QtWebApp:

                                You need to use a unique name for each connection.

                                @SGaist said in How to connect to the database correctly in QtWebApp:

                                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.

                                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
                                0

                                • Login

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