A. Yahorau
A. Yahorau

Reputation: 21

Retrieving of PostgreSQL native error code

I have installed unixODBC-2.3.1-4.95.x86_64 on my SLES 12 machine and writing some application. But I faced with problem of error handling in PosgreSQL using unixODBC.

To do this I created the following function:

void checkDiag (SQLSMALLINT handleType, SQLHANDLE handle)

{

        SQLRETURN rc = SQL_ERROR;
        SQLCHAR sqlState[6];
        SQLINTEGER nError;
        SQLSMALLINT recnum = 0;
        SQLCHAR eMsg[SQL_MAX_MESSAGE_LENGTH];
        SQLCHAR nativeError[SQL_MAX_MESSAGE_LENGTH];
        while (rc != SQL_NO_DATA_FOUND)
        {
                rc = SQLGetDiagRec (handleType,
                                    handle,
                                    recnum,
                                    sqlState,
                                    &nError,
                                    eMsg,
                                    255,
                                    NULL);
                if (rc != SQL_NO_DATA_FOUND)
                {
                        SQLGetDiagField(handleType,
                                        handle,
                                        recnum,
                                        SQL_DIAG_NATIVE,
                                        nativeError,
                                        255,
                                        NULL);
                        printf("RECNUM %d\n sqlState = %s\n nError = %d\n Error Message %s\n nativeError %s \n",
                               recnum, sqlState, nError, (char *) eMsg, (char *)nativeError);
                }
                recnum++;
        }
}

I have been trying to use different variants of diagId in SQLGetDiagField : SQL_DIAG_MESSAGE_TEXT, SQL_DIAG_NATIVE, SQL_DIAG_SQLSTATE. I got everything but not PostgreSQL error Id. I am constantly getting Postgresql error code id = -1.

So my question is:

Is it possible to get native PostgreSQL error code id using unixODBC API ? Could you please tell me what I am doing wrong?

Upvotes: 1

Views: 587

Answers (1)

A. Yahorau
A. Yahorau

Reputation: 21

I found a solution.

The contents of the config file odbcinst.ini was the following:

[PostgreSQL]
Description=PostgreSQL ODBC driver
Driver=/usr/lib64/unixODBC/libodbcpsql.so
FileUsage=1

After I changed the description of Driver entry in the configuration file I was able to get PostgreSQL native error code.

[PostgreSQL]
Description=PostgreSQL ODBC driver
Driver=/opt/PostgreSQL/psqlODBC/lib/psqlodbcw.so
FileUsage=1

psqlodbcw.so library is supplied by EnterpriseDB PostgreSQL which can be downloaded here https://www.postgresql.org/download/linux/suse/ .

After these modifications I can get native error code. Using the code provided above, the postgres error code value will be written into sqlState.

Upvotes: 1

Related Questions