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. Postgresql Return no value
Forum Updated to NodeBB v4.3 + New Features

Postgresql Return no value

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 555 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.
  • J Offline
    J Offline
    John Hon
    wrote on 13 Jan 2024, 08:50 last edited by
    #1

    Hi all,

    I have a problem to query data from my Postgresql database. My table, expenses_type, has following columns:

    expense_type char varying
    expense_id integer

    CREATE OR REPLACE FUNCTION get_expense_type_id(expense_value varchar)
      RETURNS integer
      LANGUAGE plpgsql AS
    $func$
    declare
    	max_id integer;
    BEGIN
       select expense_id into max_id from public.expenses_type where expense_type = expense_value;
       return max_id;
    END
    $func$;
    

    When I ran the following statement on pgAdmin 4,

    
    select get_expense_type_id('add')
    

    it returns value, 19. This is my expected result.

    On my Qt c++ source code, I have:

    bool FINANCIAL_DATABASE::insert_Expense_Type(const QString& expense_type, int& expense_id)
    {
     ....
    
        QSqlQuery query(m_db);
        QString expense_type1 = "add";
        query.prepare("select get_expense_type_id(:expense_type)");
        query.bindValue(":expense_type", expense_type1);
        query.exec();
        expense_id = query.value(0).toInt();
        return true;
    }
    

    query.value(0).toInt() return value, 0. The query.value(0).toInt() does not return 19 as my expected result. I cannot figure out what I did wrong here. Fyi, before I modified the above code, I implemented insert record in the same member function, the record was inserted successfully (I did not insert the code here) . Therefore, I believe the QSqlDatabase setting is correct.

    P 1 Reply Last reply 13 Jan 2024, 09:01
    0
    • P Paul Colby
      13 Jan 2024, 09:01

      Hi @John-Hon, not sure what the issue is, but all of those QSqlQuery functions return booleans, and (presumably) set relevant SQL errors on failure, so try something like:

      if (!query.prepare("select get_expense_type_id(:expense_type)")) {
          qDebug() << "prepare failed:" << query.lastError().text();
          return false;
      }
      if (!query.bindValue(":expense_type", expense_type1)) {
          qDebug() << "bind failed:" << query.lastError().text();
          return false;
      }
      if (!query.exec()) {
          qDebug() << "exec failed:" << query.lastError().text();
          return false;
      }
      

      Cheers.

      J Offline
      J Offline
      John Hon
      wrote on 13 Jan 2024, 12:56 last edited by
      #3

      @Paul-Colby said in Postgresql Return no value:

      Hi @John-Hon, not sure what the issue is, but all of those QSqlQuery functions return booleans, and (presumably) set relevant SQL errors on failure, so try something like:

      if (!query.prepare("select get_expense_type_id(:expense_type)")) {
          qDebug() << "prepare failed:" << query.lastError().text();
          return false;
      }
      if (!query.bindValue(":expense_type", expense_type1)) {
          qDebug() << "bind failed:" << query.lastError().text();
          return false;
      }
      if (!query.exec()) {
          qDebug() << "exec failed:" << query.lastError().text();
          return false;
      }
      

      Cheers.

      Fyi, the query.bindValue has no return value. I tried, I changed, query.prepare as :

      if (!query.prepare("select get get_expense_type_id(:expense_type)")) {
      qDebug() << "prepare failed:" << query.lastError().text();
          return false;
      }
      

      or

      if (!query.exec("select expense_id  from public.expenses_type where expense_type = 'add'))
      {
               qDebug() << "prepare failed:" << query.lastError().text();
              return false;
      }
      

      the above 2 codes, it does not report any errors. I ran the

      select expense_id  from public.expenses_type where expense_type = 'add'
      

      on pgadmin 4, it returns 19 but ran on the debugger mode, it returned 0. is there anything that I can miss out or should try?

      C 1 Reply Last reply 13 Jan 2024, 13:13
      0
      • J John Hon
        13 Jan 2024, 08:50

        Hi all,

        I have a problem to query data from my Postgresql database. My table, expenses_type, has following columns:

        expense_type char varying
        expense_id integer

        CREATE OR REPLACE FUNCTION get_expense_type_id(expense_value varchar)
          RETURNS integer
          LANGUAGE plpgsql AS
        $func$
        declare
        	max_id integer;
        BEGIN
           select expense_id into max_id from public.expenses_type where expense_type = expense_value;
           return max_id;
        END
        $func$;
        

        When I ran the following statement on pgAdmin 4,

        
        select get_expense_type_id('add')
        

        it returns value, 19. This is my expected result.

        On my Qt c++ source code, I have:

        bool FINANCIAL_DATABASE::insert_Expense_Type(const QString& expense_type, int& expense_id)
        {
         ....
        
            QSqlQuery query(m_db);
            QString expense_type1 = "add";
            query.prepare("select get_expense_type_id(:expense_type)");
            query.bindValue(":expense_type", expense_type1);
            query.exec();
            expense_id = query.value(0).toInt();
            return true;
        }
        

        query.value(0).toInt() return value, 0. The query.value(0).toInt() does not return 19 as my expected result. I cannot figure out what I did wrong here. Fyi, before I modified the above code, I implemented insert record in the same member function, the record was inserted successfully (I did not insert the code here) . Therefore, I believe the QSqlDatabase setting is correct.

        P Offline
        P Offline
        Paul Colby
        wrote on 13 Jan 2024, 09:01 last edited by
        #2

        Hi @John-Hon, not sure what the issue is, but all of those QSqlQuery functions return booleans, and (presumably) set relevant SQL errors on failure, so try something like:

        if (!query.prepare("select get_expense_type_id(:expense_type)")) {
            qDebug() << "prepare failed:" << query.lastError().text();
            return false;
        }
        if (!query.bindValue(":expense_type", expense_type1)) {
            qDebug() << "bind failed:" << query.lastError().text();
            return false;
        }
        if (!query.exec()) {
            qDebug() << "exec failed:" << query.lastError().text();
            return false;
        }
        

        Cheers.

        J 1 Reply Last reply 13 Jan 2024, 12:56
        2
        • P Paul Colby
          13 Jan 2024, 09:01

          Hi @John-Hon, not sure what the issue is, but all of those QSqlQuery functions return booleans, and (presumably) set relevant SQL errors on failure, so try something like:

          if (!query.prepare("select get_expense_type_id(:expense_type)")) {
              qDebug() << "prepare failed:" << query.lastError().text();
              return false;
          }
          if (!query.bindValue(":expense_type", expense_type1)) {
              qDebug() << "bind failed:" << query.lastError().text();
              return false;
          }
          if (!query.exec()) {
              qDebug() << "exec failed:" << query.lastError().text();
              return false;
          }
          

          Cheers.

          J Offline
          J Offline
          John Hon
          wrote on 13 Jan 2024, 12:56 last edited by
          #3

          @Paul-Colby said in Postgresql Return no value:

          Hi @John-Hon, not sure what the issue is, but all of those QSqlQuery functions return booleans, and (presumably) set relevant SQL errors on failure, so try something like:

          if (!query.prepare("select get_expense_type_id(:expense_type)")) {
              qDebug() << "prepare failed:" << query.lastError().text();
              return false;
          }
          if (!query.bindValue(":expense_type", expense_type1)) {
              qDebug() << "bind failed:" << query.lastError().text();
              return false;
          }
          if (!query.exec()) {
              qDebug() << "exec failed:" << query.lastError().text();
              return false;
          }
          

          Cheers.

          Fyi, the query.bindValue has no return value. I tried, I changed, query.prepare as :

          if (!query.prepare("select get get_expense_type_id(:expense_type)")) {
          qDebug() << "prepare failed:" << query.lastError().text();
              return false;
          }
          

          or

          if (!query.exec("select expense_id  from public.expenses_type where expense_type = 'add'))
          {
                   qDebug() << "prepare failed:" << query.lastError().text();
                  return false;
          }
          

          the above 2 codes, it does not report any errors. I ran the

          select expense_id  from public.expenses_type where expense_type = 'add'
          

          on pgadmin 4, it returns 19 but ran on the debugger mode, it returned 0. is there anything that I can miss out or should try?

          C 1 Reply Last reply 13 Jan 2024, 13:13
          0
          • J John Hon
            13 Jan 2024, 12:56

            @Paul-Colby said in Postgresql Return no value:

            Hi @John-Hon, not sure what the issue is, but all of those QSqlQuery functions return booleans, and (presumably) set relevant SQL errors on failure, so try something like:

            if (!query.prepare("select get_expense_type_id(:expense_type)")) {
                qDebug() << "prepare failed:" << query.lastError().text();
                return false;
            }
            if (!query.bindValue(":expense_type", expense_type1)) {
                qDebug() << "bind failed:" << query.lastError().text();
                return false;
            }
            if (!query.exec()) {
                qDebug() << "exec failed:" << query.lastError().text();
                return false;
            }
            

            Cheers.

            Fyi, the query.bindValue has no return value. I tried, I changed, query.prepare as :

            if (!query.prepare("select get get_expense_type_id(:expense_type)")) {
            qDebug() << "prepare failed:" << query.lastError().text();
                return false;
            }
            

            or

            if (!query.exec("select expense_id  from public.expenses_type where expense_type = 'add'))
            {
                     qDebug() << "prepare failed:" << query.lastError().text();
                    return false;
            }
            

            the above 2 codes, it does not report any errors. I ran the

            select expense_id  from public.expenses_type where expense_type = 'add'
            

            on pgadmin 4, it returns 19 but ran on the debugger mode, it returned 0. is there anything that I can miss out or should try?

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 13 Jan 2024, 13:13 last edited by
            #4

            @John-Hon said in Postgresql Return no value:

            that I can miss

            You simply did not read the documentation on how it works. QSqlQuery::exec() executes the query, the data is fetched with QSqlQuery::next().

            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
            2
            • J John Hon has marked this topic as solved on 13 Jan 2024, 13:46

            1/4

            13 Jan 2024, 08:50

            • Login

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