public inbox for [email protected]  
help / color / mirror / Atom feed
From: Igor Korot <[email protected]>
To: pgsql-generallists.postgresql.org <[email protected]>
Subject: How to get the data from the query
Date: Sat, 26 Apr 2025 19:18:03 -0500
Message-ID: <CA+FnnTxPsPRzOtLGh7389gwjS3tOJaLpQ3Jhxi26UOr9BW5a+g@mail.gmail.com> (raw)

Hi, ALL,

[code]
    std::wstring query2 = L"SELECT DISTINCT column_name, data_type,
character_maximum_length, character_octet_length, numeric_precision,
numeric_precision_radix, numeric_scale, is_nullable, column_default,
CASE WHEN column_name IN (SELECT ccu.column_name FROM
information_schema.constraint_column_usage ccu,
information_schema.table_constraints tc WHERE ccu.constraint_name =
tc.constraint_name AND tc.constraint_type = 'PRIMARY KEY' AND
ccu.table_name = 'leagues') THEN 'YES' ELSE 'NO' END AS is_pk,
ordinal_position FROM information_schema.columns col,
information_schema.table_constraints tc WHERE tc.table_schema =
col.table_schema AND tc.table_name = col.table_name AND
col.table_schema = $1 AND col.table_name = $2 ORDER BY
ordinal_position;";

            res2 = PQprepare( m_db, "get_columns",
m_pimpl->m_myconv.to_bytes( query2.c_str() ).c_str(), 2, NULL );
            if( PQresultStatus( res2 ) != PGRES_COMMAND_OK )
            {
                std::wstring err = m_pimpl->m_myconv.from_bytes(
PQerrorMessage( m_db ) );
                errorMsg.push_back( L"Error executing query: " + err );
                PQclear( res2 );
                result = 1;
            }
            else
            {
                PQclear( res2 );
......

        res2 = PQexecPrepared( m_db, "get_columns", 2, values1,
length1, formats1, 1 );
        status = PQresultStatus( res2 );
        if( status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK )
        {
            std::wstring err = m_pimpl->m_myconv.from_bytes(
PQerrorMessage( m_db ) );
            errorMsg.push_back( L"Error executing query: " + err );
            PQclear( res2 );
            fields.erase( fields.begin(), fields.end() );
            foreign_keys.erase( foreign_keys.begin(), foreign_keys.end() );
            result = 1;
        }
        else if( status == PGRES_TUPLES_OK )
        {
            int numFields = 0;
            for( int j = 0; j < PQntuples( res2 ); j++ )
            {
                int size, precision;
                bool autoinc = false;
                const char *field_name = PQgetvalue( res2, j, 0 );
                fieldName = m_pimpl->m_myconv.from_bytes( field_name );
                fieldType = m_pimpl->m_myconv.from_bytes( PQgetvalue(
res2, j, 1 ) );
                char *char_length = PQgetvalue( res2, j, 2 );
                char *numeric_length = PQgetvalue( res2, j, 4 );
                char *numeric_scale = PQgetvalue( res2, j, 6 );
                fieldDefaultValue = m_pimpl->m_myconv.from_bytes(
PQgetvalue( res2, j, 8 ) );
                fieldIsNull = !strcmp( PQgetvalue( res2, j, 7 ), "YES"
) ? 1 : 0;
                fieldPK = !strcmp( PQgetvalue( res2, j, 9 ), "YES" ) ? 1 : 0;
                if( *char_length == '0' )
                {
                    size = atoi( numeric_length );
                    precision = atoi( numeric_scale );
                }
                else
                {
                    size = atoi( char_length );
                    precision = 0;
                }
                if( fieldType == L"serial" || fieldType == L"bigserial" )
                    autoinc = true;
[/code]

I'm able to get field name and field type, but the size comes out "" (empty)
.

WHat am I doing wrong?

Thank you.






reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: How to get the data from the query
  In-Reply-To: <CA+FnnTxPsPRzOtLGh7389gwjS3tOJaLpQ3Jhxi26UOr9BW5a+g@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox