Quantcast
Channel: SCN: Message List - SAP Adaptive Server Enterprise (SAP ASE) for Custom Applications
Viewing all articles
Browse latest Browse all 3587

Re: Using python to catch server messages

$
0
0

Hi Alexander,

 

I hope this will look ok when posted! I assume you meant code in ODBC API.Below I point out something for python...

 

I'll provide sample using SQLDriverConnect:

 

ret = SQLDriverConnect(dbc, NULL, instr, SQL_NTS, outstr, sizeof(outstr), &outstrlen,

   SQL_DRIVER_COMPLETE);   //;SQL_DRIVER_PROMPT);

 

if (SQL_SUCCEEDED(ret))

{

printf("Connected with connection #1 using the following connection string:\n\t%s\n", instr);

printf("Returned connection string was:\n\t%s\n", outstr);

if (ret == SQL_SUCCESS_WITH_INFO)

{

printf("Driver reported the following diagnostics\n");

OdbcWarning("SQLDriverConnect: Connection warnings", SQL_HANDLE_DBC, dbc, ret );

}

}

else

ErrorExit("SQLDriverConnect: Connection failed", SQL_HANDLE_DBC, dbc, ret);

 

In the above, if return is SQL_SUCCESS_WITH_INFO, call OdbcWarning().  This uses the SQLGetDiagRec API call to get the message information. Then I use function DisplayWarning to print out the information.

 

ODBCUTIL_API void OdbcWarning( char *msg, SQLSMALLINT hanType, SQLHANDLE odbcHandle, RETCODE retCode)

{

 

SQLSMALLINT  i = 1;  // record count
SQLRETURN  retcode;
SQLINTEGER  NativeError;
SQLCHAR   Msg[SQL_MAX_MESSAGE_LENGTH];
SQLCHAR   *SqlState;  //[SQL_SQLSTATE_SIZE];
SQLSMALLINT     MsgLen;
//SQLRETURN     nErrResult;             // Return Code from SQLGetDiagRec

SqlState = (SQLCHAR *)malloc(SQL_SQLSTATE_SIZE * sizeof(SQLCHAR));
printf("Warning occured in %s\n", msg);

 

// Use SQLGetDiagRec
while ( (retcode = SQLGetDiagRec(hanType,   odbcHandle, i,
    SqlState,
    &NativeError,
    Msg,
    sizeof(Msg),
    &MsgLen) ) != SQL_NO_DATA)
{


  DisplayWarning(SqlState, NativeError, Msg, MsgLen);
  i++;


}

// debug purpose
cout << "\nOdbcWarning: retcode: " << retcode << ", i: " << i - 1 << endl;


if (retcode == SQL_NO_DATA && i == 1)

{

  cout << "No message to print, retcode SQLGetDiagRec is SQL_NO_DATA" << endl;
  exit(1);

}

}

 

 

void DisplayWarning( SQLCHAR sqlState[SQL_SQLSTATE_SIZE], SQLINTEGER nativeErr, SQLCHAR msg[SQL_MAX_MESSAGE_LENGTH], SQLSMALLINT msgLen)

{

printf("Warning: SQLState: %s, Native Error: %d, Message: %s, Message Length: %d\n",

   sqlState, nativeErr, msg, msgLen);

 

}

 

 

This is a good way to get information that is returned from ASE that isn't a result set.

 

If you coding Transact-SQL print output or things like statistics io, etc, when you execute you test for SQL_SUCCESS_WITH_INFO and then use the OdbcWarning code to grab the output and provide to application.

 

You can also Do similar function for SQL_ERROR as well.

 

In the sybpydb module, in the docs:

 

Warning and Error Messages

 

However, I have no samples - if you need this let me know - I'll have to ask engineering how to do it.

 

I setup something and get syntax error:

 

try:

conn = sybpydb.connect(user='sa', password='secret')

except sybpydb/Warning,w:

print 'Warning: %s' %(w)

 

 

  File "firstapp_warning.py", line 55

    except sybpydb/Warning,w:

                          ^

SyntaxError: invalid syntax

 

Cheers,

-Paul


Viewing all articles
Browse latest Browse all 3587

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>