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. Problem with query
Forum Update on Monday, May 27th 2025

Problem with query

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 608 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.
  • K Offline
    K Offline
    Kuji
    wrote on 2 Jan 2023, 18:49 last edited by Kuji 1 Feb 2023, 19:14
    #1

    This is my table872300e6-f387-4e6e-bfc5-be87c9af9cd8-image.png
    I connect to database and try to get data by using query

                QSqlQuery qry;
                qry.prepare("SELECT * FROM user");
                qInfo()<<qry.lastError();
                qry.exec();
                qInfo()<<qry.lastError();
                if(qry.size()<1)
                    qInfo()<<"no data";
    
                else
                    while (qry.next())
                    {
                        qInfo()<<qry.value(0).toString();
                    }
    

    After this i got this in console
    QSqlError("", "", "")
    QSqlError("", "", "")
    "postgres"

    I tryed take one column with this code

                QSqlQuery qry;
                qry.prepare("SELECT age FROM user");
                qInfo()<<qry.lastError();
                qry.exec();
                qInfo()<<qry.lastError();
                if(qry.size()<1)
                    qInfo()<<"no data";
    
                else
                    while (qry.next())
                    {
                        qInfo()<<qry.value(0).toString();
                    }
    

    After this i got this in console

    QSqlError("42703", "QPSQL: Unable to prepare statement", "ERROR: column "age" doesn't exist\nLINE 1: PREPARE qpsqlpstmt_1 AS SELECT age FROM user\n ^\n(42703)")
    QSqlError("42601", "QPSQL: Unable to create query", "ERROR: syntax error at or near\nLINE 1: EXECUTE \n ^\n(42601)")
    no data

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 2 Jan 2023, 22:02 last edited by
      #6

      Your first code block (that returned something) with some comments inserted:

                  QSqlQuery qry;
                  qry.prepare("SELECT * FROM user");
                  qInfo()<<qry.lastError();
      

      Postgresql has a reserved word user. Best not to use that name for a table of your own. It can be done but is more trouble than it is worth.

                  qInfo()<<qry.lastError();
      

      OK, it executed something.

                  if(qry.size()<1)
                      qInfo()<<"no data";
      

      See QSqlQuery::size() for reasons this might be unreliable. I do not know if it is, or is not, under Postgresql.

                  else
                      while (qry.next())
                      {
                          qInfo()<<qry.value(0).toString();
                      }
      

      This prints "postgres". That value does not appear anywhere in the table you showed us a picture of ( @SGaist's point). Either using the reserved word user has resulted in this, possibly user maps to the pg_user system catalog, or your default QSqlDatabase connection is not connected to the database you think it is.

      K 1 Reply Last reply 2 Jan 2023, 23:15
      3
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 2 Jan 2023, 18:56 last edited by
        #2

        The error message does not match the query you show us above

        PREPARE qpsqlpstmt_1 AS SELECT * FROM user WHERE password = ...

        ' PREPARE qpsqlpstmt_1 AS' is added for a prepared (postgresql) statement by the driver but the rest comes from your code. so post the correct code which triggers the error and/or fix it - as you can see your table has no column password.

        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
        • K Offline
          K Offline
          Kuji
          wrote on 2 Jan 2023, 19:16 last edited by Kuji 1 Feb 2023, 19:16
          #3

          @Christian-Ehrlicher Changed console errors

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 2 Jan 2023, 19:20 last edited by
            #4

            Hi,

            From the looks of it, you are not accessing the table you think you are accessing.

            From the "successful" query, you are not reading the table you are showing in your picture.

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

            K 1 Reply Last reply 2 Jan 2023, 21:04
            0
            • S SGaist
              2 Jan 2023, 19:20

              Hi,

              From the looks of it, you are not accessing the table you think you are accessing.

              From the "successful" query, you are not reading the table you are showing in your picture.

              K Offline
              K Offline
              Kuji
              wrote on 2 Jan 2023, 21:04 last edited by
              #5

              @SGaist Do you know how can i access?

              1 Reply Last reply
              0
              • C Offline
                C Offline
                ChrisW67
                wrote on 2 Jan 2023, 22:02 last edited by
                #6

                Your first code block (that returned something) with some comments inserted:

                            QSqlQuery qry;
                            qry.prepare("SELECT * FROM user");
                            qInfo()<<qry.lastError();
                

                Postgresql has a reserved word user. Best not to use that name for a table of your own. It can be done but is more trouble than it is worth.

                            qInfo()<<qry.lastError();
                

                OK, it executed something.

                            if(qry.size()<1)
                                qInfo()<<"no data";
                

                See QSqlQuery::size() for reasons this might be unreliable. I do not know if it is, or is not, under Postgresql.

                            else
                                while (qry.next())
                                {
                                    qInfo()<<qry.value(0).toString();
                                }
                

                This prints "postgres". That value does not appear anywhere in the table you showed us a picture of ( @SGaist's point). Either using the reserved word user has resulted in this, possibly user maps to the pg_user system catalog, or your default QSqlDatabase connection is not connected to the database you think it is.

                K 1 Reply Last reply 2 Jan 2023, 23:15
                3
                • C ChrisW67
                  2 Jan 2023, 22:02

                  Your first code block (that returned something) with some comments inserted:

                              QSqlQuery qry;
                              qry.prepare("SELECT * FROM user");
                              qInfo()<<qry.lastError();
                  

                  Postgresql has a reserved word user. Best not to use that name for a table of your own. It can be done but is more trouble than it is worth.

                              qInfo()<<qry.lastError();
                  

                  OK, it executed something.

                              if(qry.size()<1)
                                  qInfo()<<"no data";
                  

                  See QSqlQuery::size() for reasons this might be unreliable. I do not know if it is, or is not, under Postgresql.

                              else
                                  while (qry.next())
                                  {
                                      qInfo()<<qry.value(0).toString();
                                  }
                  

                  This prints "postgres". That value does not appear anywhere in the table you showed us a picture of ( @SGaist's point). Either using the reserved word user has resulted in this, possibly user maps to the pg_user system catalog, or your default QSqlDatabase connection is not connected to the database you think it is.

                  K Offline
                  K Offline
                  Kuji
                  wrote on 2 Jan 2023, 23:15 last edited by
                  #7

                  @ChrisW67 Thank you this helped.

                  1 Reply Last reply
                  0

                  1/7

                  2 Jan 2023, 18:49

                  • Login

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