Fwd: Problem retrieving data

Поиск
Список
Период
Сортировка
Искать

Fwd: Problem retrieving data

От:
Igor Korot <ikorot01@gmail.com>
Дата:
I initially sent it to the main PG list.

But I think it is more blong tothe ODBC one

---------- Forwarded message ---------
From: Igor Korot 
Date: Sun, Nov 15, 2026 at 1:11 AM
Subject: Problem retrieving data
To: pgsql-generallists.postgresql.org 


Hi, ALL,

I don't know if this belongs here or in the ODBC list. I'm going to
try this first...

[code]
igor@WaylandGnome ~/dbhandler $ psql -U postgres
Password for user postgres:
psql (16.2)
Type "help" for help.

postgres=# \c draft
You are now connected to database "draft" as user "postgres".
draft=# SELECT * FROM abcatedt;
draft=# \d abcatedt
                      Table "public.abcatedt"
  Column  |          Type          | Collation | Nullable | Default
----------+------------------------+-----------+----------+---------
 abe_name | character varying(30)  |           | not null |
 abe_edit | character varying(254) |           |          |
 abe_type | smallint               |           |          |
 abe_cntr | integer                |           |          |
 abe_seqn | smallint               |           | not null |
 abe_flag | integer                |           |          |
 abe_work | character varying(32)  |           |          |
Indexes:
    "abcate_x" UNIQUE, btree (abe_name, abe_seqn)

draft=#
[/code]

[code]
        abe_name         |         abe_edit         | abe_type |
abe_cntr | abe_seqn |  abe_flag  | abe_work
--------------------------+--------------------------+----------+----------+----------+------------+----------
 ###-##-####              | ###-##-####              |       90 |
  1 |        1 |         32 | 00
 ###,###.00               | ###,###.00               |       90 |
  1 |        1 |         32 | 10
 #####                    | #####                    |       90 |
  1 |        1 |         32 | 10
 BenefitsCheckBox         |                          |       85 |
  4 |        1 |  536870916 |
 BenefitsCheckBox         | Y                        |       85 |
  4 |        2 |          0 |
 BenefitsCheckBox         | N                        |       85 |
  4 |        3 |          0 |
 Color List               | 0                        |       87 |
  3 |        1 | -201326590 |
 Color List               | White                    |       87 |
  3 |        2 |          0 |
 Color List               | White                    |       87 |
  3 |        3 |          0 |
 Color List               | Black                    |       87 |
  3 |        4 |          0 |
 Color List               | Black                    |       87 |
  3 |        5 |          0 |
 Color List               | Purple                   |       87 |
  3 |        6 |          0 |
 Color List               | Purple                   |       87 |
  3 |        7 |          0 |
 Color List               | Orange                   |       87 |
  3 |        8 |          0 |
 Color List               | Orange                   |       87 |
  3 |        9 |          0 |
 Color List               | Green                    |       87 |
  3 |       10 |          0 |
 Color List               | Green                    |       87 |
  3 |       11 |          0 |
 Color List               | Blue                     |       87 |
  3 |       12 |          0 |
 Color List               | Blue                     |       87 |
  3 |       13 |          0 |
 Color List               | Yellow                   |       87 |
  3 |       14 |          0 |
 Color List               | Yellow                   |       87 |
  3 |       15 |          0 |
lines 1-23
[/code]

As you can see everything is retrieved correctly.

Now in my program I am using unixODBC with the ODBC driver.

This is the function I'm running:

[code]
int ODBCDatabase::GetFieldStyles(std::map > > &styles,
std::vector &errorMsg)
{
    SQLWCHAR styleName[32], work[33], styleStyle[255];
    int styleType = 0, seqn = 0, flag = 0;
    SQLLEN cbStyleName = SQL_NTS, cbStyleType = 0, cbSeqn = 0, cbFlag
= 0, cbWork = SQL_NTS, cbStyleStyle = SQL_NTS;
    int result = 0;
    std::wstring query = L"SELECT abe_name, abe_type, abe_seqn,
abe_edit, abe_flag, abe_work FROM abcatedt";
    std::unique_ptr qry( new SQLWCHAR[query.length() + 2] );
    memset( qry.get(), '\0', query.length() + 2 );
    uc_to_str_cpy( qry.get(), query );
    auto ret = SQLAllocHandle( SQL_HANDLE_STMT, m_hdbc, &m_hstmt );
    if( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO )
    {
        GetErrorMessage( errorMsg, CONN_ERROR );
        result = 1;
    }
    if( !result )
    {
        ret = SQLPrepare( m_hstmt, qry.get(), SQL_NTS );
        if( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO )
        {
            GetErrorMessage( errorMsg, STMT_ERROR );
            result = 1;
        }
    }
    if( !result )
    {
        ret = SQLExecute( m_hstmt );
        if( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO )
        {
            GetErrorMessage( errorMsg, STMT_ERROR );
            result = 1;
        }
    }
    if( !result )
    {
        ret = SQLBindCol( m_hstmt, 1, SQL_C_WCHAR, &styleName, 33,
&cbStyleName );
        if( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO )
        {
            GetErrorMessage( errorMsg, STMT_ERROR );
            result = 1;
        }
    }
    if( !result )
    {
        ret = SQLBindCol( m_hstmt, 2, SQL_C_USHORT, &styleType, 0,
&cbStyleType );
        if( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO )
        {
            GetErrorMessage( errorMsg, STMT_ERROR );
            result = 1;
        }
    }
    if( !result )
    {
        ret = SQLBindCol( m_hstmt, 3, SQL_C_USHORT, &seqn, 0, &cbSeqn );
        if( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO )
        {
            GetErrorMessage( errorMsg, STMT_ERROR );
            result = 1;
        }
    }
    if( !result )
    {
        ret = SQLBindCol( m_hstmt, 4, SQL_C_WCHAR, &styleStyle, 33,
&cbStyleStyle );
        if( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO )
        {
            GetErrorMessage( errorMsg, STMT_ERROR );
            result = 1;
        }
    }
    if( !result )
    {
        ret = SQLBindCol( m_hstmt, 5, SQL_C_USHORT, &flag, 0, &cbFlag );
        if( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO )
        {
            GetErrorMessage( errorMsg, STMT_ERROR );
            result = 1;
        }
    }
    if( !result )
    {
        ret = SQLBindCol( m_hstmt, 6, SQL_C_WCHAR, &work, 33, &cbWork );
        if( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO )
        {
            GetErrorMessage( errorMsg, STMT_ERROR );
            result = 1;
        }
    }
    for( ret = SQLFetch( m_hstmt ); ( ret == SQL_SUCCESS || ret ==
SQL_SUCCESS_WITH_INFO ) && ret != SQL_NO_DATA; ret = SQLFetch( m_hstmt
) )
    {
        std::wstring name, style, styleWork;
        str_to_uc_cpy( name, styleName );
        str_to_uc_cpy( styleWork, work );
        str_to_uc_cpy( style, styleStyle );
        styles[name].emplace_back( std::make_shared(
styleType, seqn, style, flag, styleWork ) );
    }
    if( result == 1 )
    {
        ret = SQLEndTran( SQL_HANDLE_DBC, m_hdbc, SQL_ROLLBACK );
    }
    else
    {
        ret = SQLEndTran( SQL_HANDLE_DBC, m_hdbc, SQL_COMMIT );
    }
    if( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO )
    {
        GetErrorMessage( errorMsg, CONN_ERROR );
        result = 1;
    }
    if( !result )
    {
        ret = SQLFreeHandle( SQL_HANDLE_STMT, m_hstmt );
        if( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO )
        {
            GetErrorMessage( errorMsg, STMT_ERROR );
            result = 1;
        }
        m_hstmt = 0;
    }
    return result;
}
[/code]

and this is what I'm receiving:

[code]
Thread 1 "dbhandler" hit Breakpoint 2, ODBCDatabase::GetFieldStyles
(this=0x616000154880, styles=std::map with 4 elements = {...},
    errorMsg=std::vector of length 0, capacity 0) at
../../libodbc/database_odbc.cpp:10710
10710        std::wstring name, style, styleWork;
(gdb) n
10711        str_to_uc_cpy( name, styleName );
(gdb)
10712        str_to_uc_cpy( styleWork, work );
(gdb) p name
$5 = L"BenefitsCheckBo"
(gdb) p cbStyleName
$6 = 32
(gdb) p styleName
$7 = {66, 101, 110, 101, 102, 105, 116, 115, 67, 104, 101, 99, 107,
66, 111, 0, 46832, 22, 24880, 0, 57173, 62688, 32767, 0, 0, 0, 0, 0,
46816,
  22, 24880, 0}
(gdb) p (char *) styleName
$8 = 0x7fffffffbca0 "B"
(gdb) p (char *) styleName[1]
$9 = 0x65 
(gdb) p (char) styleName[1]
$10 = 101 'e'
(gdb) p (char) styleName[2]
$11 = 110 'n'
(gdb) p (char) styleName[3]
$12 = 101 'e'
(gdb) p (char) styleName[4]
$13 = 102 'f'
(gdb) p (char) styleName[5]
$14 = 105 'i'
(gdb) p (char) styleName[6]
$15 = 116 't'
(gdb) p (char) styleName[7]
$16 = 115 's'
(gdb) p (char) styleName[8]
$17 = 67 'C'
(gdb) p (char) styleName[9]
$18 = 104 'h'
(gdb) p (char) styleName[10]
$19 = 101 'e'
(gdb) p (char) styleName[11]
$20 = 99 'c'
(gdb) p (char) styleName[12]
$21 = 107 'k'
(gdb) p (char) styleName[13]
$22 = 66 'B'
(gdb) p (char) styleName[14]
$23 = 111 'o'
(gdb) p (char) styleName[15]
$24 = 0 '\000'
[/code]

The data is cut off.

Should I forward this to the PG-ODBC ML?

Could someone here confirm/take a look at this?

Thank you.


Re: Fwd: Problem retrieving data

От:
Nick Gorham <nick@lurcherlink.org>
Дата:
On 15/11/2026 08:12, Igor Korot wrote:
ret = SQLBindCol( m_hstmt, 1, SQL_C_WCHAR, &styleName, 33,
&cbStyleName );

That should be

ret = SQLBindCol( m_hstmt, 1, SQL_C_WCHAR, &styleName, sizeof(styleName) ,&cbStyleName );

The buffer length argument is always the size of the buffer in bytes, not in characters (in this case).

In this case sizeof(styleName) will be 66, not 33;

-- 
Nick Gorham


FAQ