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. QPSQL: Unable to create query
Forum Updated to NodeBB v4.3 + New Features

QPSQL: Unable to create query

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 2 Posters 1.4k 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on 10 May 2020, 12:26 last edited by
    #1

    Hi!
    I I get error: QPSQL: "42601", "QPSQL: Unable to create query".
    How do I fix this?

    void DataBase::openDB()
    {
        dataBase = QSqlDatabase::addDatabase("QPSQL");
        dataBase.setDatabaseName("db_taxi_vik_park");
        //ResumeDB.setDatabaseName("1");
        dataBase.setUserName("postgres");
        dataBase.setPassword("1qaz");
        //ResumeDB.setHostName("127.0.0.1");
        dataBase.setPort(5433);//*/
        if (dataBase.open()) {
            qDebug()<<"db is opened \r\n";}
        else {
            qDebug()<<"db not opened \r\n";}
    }
    
    QVector<QMap<QString, QString> > DataBase::getAllAccauntWithoutPhoto()
    {
        QVector<QMap<QString, QString> > resultVector;
        QMap<QString, QString> valueMap;
        QSqlQuery query;
        query.prepare("SELECT "
                      "id, "
                      "name, "
                      "email, "
                      "login, "
                      "password, "
                      "date_create, "
                      "date_block, "
                      "id_key_device, "
                      "number_of_downloaded_games, "
                      "blocked_bool, "
                      "comment "
                      " FROM public.account "
                      " ;");
    
        if (!query.exec()) {qDebug()<<"not get data from db \r\n"<<query.lastError()<<"\r\n";}
        else {
            qDebug()<<"get data from db \r\n";
            query.first();
    
            valueMap["id"] = query.value(0).toString();
            valueMap["name"] = query.value(1).toString();
            valueMap["email"] = query.value(2).toString();
            valueMap["login"] = query.value(3).toString();
            valueMap["password"] = query.value(4).toString();
            valueMap["date_create"] = query.value(5).toString();
            valueMap["date_block"] = query.value(6).toString();
            valueMap["id_key_device"] = query.value(7).toString();
            valueMap["number_of_downloaded_games"] = query.value(8).toString();
            valueMap["blocked_bool"] = query.value(9).toString();
            valueMap["comment"] = query.value(10).toString();
    
            resultVector.append(valueMap);
    
            while (query.next()) {
                valueMap["id"] = query.value(0).toString();
                valueMap["name"] = query.value(1).toString();
                valueMap["email"] = query.value(2).toString();
                valueMap["login"] = query.value(3).toString();
                valueMap["password"] = query.value(4).toString();
                valueMap["date_create"] = query.value(5).toString();
                valueMap["date_block"] = query.value(6).toString();
                valueMap["id_key_device"] = query.value(7).toString();
                valueMap["number_of_downloaded_games"] = query.value(8).toString();
                valueMap["blocked_bool"] = query.value(9).toString();
                valueMap["comment"] = query.value(10).toString();
                resultVector.append(valueMap);
            }
        }
        return resultVector;
    }
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 10 May 2020, 12:33 last edited by Christian Ehrlicher 5 Oct 2020, 12:33
      #2

      By looking what this error means: https://www.postgresql.org/docs/10/errcodes-appendix.html

      Try to run your query directly with psql to see where exactly the error is.
      Also there is no need to use prepare() here since you don't bind any values.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      M 2 Replies Last reply 10 May 2020, 12:44
      2
      • C Christian Ehrlicher
        10 May 2020, 12:33

        By looking what this error means: https://www.postgresql.org/docs/10/errcodes-appendix.html

        Try to run your query directly with psql to see where exactly the error is.
        Also there is no need to use prepare() here since you don't bind any values.

        M Offline
        M Offline
        Mikeeeeee
        wrote on 10 May 2020, 12:44 last edited by
        #3

        @Christian-Ehrlicher I tried making a query in psql and the query works.
        I also tried to make this request

            QSqlQuery query("select * from public.account");
            QSqlError error = query.lastError();
            if (error.type() == QSqlError::NoError) {
                while(query.next())
                {
                    qDebug() << query.value(0).toString();
                }
            }
            else {
                qDebug() << error.text();
            }
        

        But I got the error of not having a table, whereas the table is exactly there.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 10 May 2020, 13:59 last edited by
          #4

          The table is 'account', the schema public - try without schema

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          M 1 Reply Last reply 10 May 2020, 15:01
          0
          • C Christian Ehrlicher
            10 May 2020, 12:33

            By looking what this error means: https://www.postgresql.org/docs/10/errcodes-appendix.html

            Try to run your query directly with psql to see where exactly the error is.
            Also there is no need to use prepare() here since you don't bind any values.

            M Offline
            M Offline
            Mikeeeeee
            wrote on 10 May 2020, 14:19 last edited by
            #5

            @Christian-Ehrlicher Can there be a problem with a bad driver?

            1 Reply Last reply
            0
            • C Christian Ehrlicher
              10 May 2020, 13:59

              The table is 'account', the schema public - try without schema

              M Offline
              M Offline
              Mikeeeeee
              wrote on 10 May 2020, 15:01 last edited by
              #6

              @Christian-Ehrlicher it is too not work:

              QSqlQuery query("SELECT *  FROM account ", dataBase);
              
              
                  if (!query.exec()) {
                      qDebug()<<"not get data from db \r\n"<<query.lastError()<<"\r\n";
                  qDebug()<<query.lastQuery();
                  }
                  else {
                      qDebug()<<"get data from db \r\n";
                      query.first();
              
              
              
                  }
                  return resultVector;
              
              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mikeeeeee
                wrote on 10 May 2020, 15:04 last edited by
                #7

                I use psql 12 in the windows 7 64

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 10 May 2020, 16:43 last edited by
                  #8

                  So where do you get the error? during query.exec()? And what error? The same as in the first post?

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mikeeeeee
                    wrote on 10 May 2020, 16:57 last edited by
                    #9

                    @Christian-Ehrlicher said in QPSQL: Unable to create query:

                    query.exec()

                    Yes, in query.exec(). The same error. Looks like it's the driver. How do I find out which version of the driver is designed for?

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Mikeeeeee
                      wrote on 10 May 2020, 17:19 last edited by
                      #10

                      Do you connect PSQL to the project and it works normally?

                      C 1 Reply Last reply 10 May 2020, 17:21
                      0
                      • M Mikeeeeee
                        10 May 2020, 17:19

                        Do you connect PSQL to the project and it works normally?

                        C Offline
                        C Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 10 May 2020, 17:21 last edited by
                        #11

                        @Mikeeeeee I'm using psql since ages with Qt. Imo it's a problem with your psql installation. Simply test it with a sqlite database. Also you should take a look what others did to solve the psql 42601 error but that's not Qt related

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          Mikeeeeee
                          wrote on 10 May 2020, 17:35 last edited by
                          #12

                          Before the update, I had PSQL working, after the update, new drivers were added and it stopped working.

                          1 Reply Last reply
                          0
                          • C Offline
                            C Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 10 May 2020, 17:43 last edited by
                            #13

                            Thenswitch back to the old PostgreSQL - or do you really need some new features in PostgreSQL 12 (btw: psql 12 is supported since Qt5.14 and works fine here)

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            1
                            • M Offline
                              M Offline
                              Mikeeeeee
                              wrote on 10 May 2020, 19:00 last edited by
                              #14

                              Sorry, I'm in a connection indicated is not the base, my stupid mistake.

                              1 Reply Last reply
                              0

                              1/14

                              10 May 2020, 12:26

                              • Login

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