Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
stefan_schnell
Active Contributor
I presented until now in the series "How to use SAPNetWeaver RFC Library with Pyhton" :

Here now an example how to read a table with the function module (FM) RFC_READ_TABLE. You need the file sapnwrfc.py from here - look at the end of the posting.


After the connection we get the function description of the FM, in our case RFC_READ_TABLE. We set the arguments QUERY_TABLE, in our case USR01, and the DELIMITER. We invoke the FM and print the result line by line. The result is in the DATA table, in the field WA.



# -*- coding: iso-8859-15 -*-
#-Begin-----------------------------------------------------------------

#-Include---------------------------------------------------------------
FileName = "sapnwrfc.py"
exec(compile(open(FileName).read(), FileName, "exec"))

#-Main------------------------------------------------------------------

#-Connection parameters-------------------------------------------------
RfcConnParams[0].name = "ASHOST"; RfcConnParams[0].value = "ABAP"
RfcConnParams[1].name = "SYSNR" ; RfcConnParams[1].value = "00"
RfcConnParams[2].name = "CLIENT"; RfcConnParams[2].value = "001"
RfcConnParams[3].name = "USER" ; RfcConnParams[3].value = "BCUSER"
RfcConnParams[4].name = "PASSWD"; RfcConnParams[4].value = "minisap"

TableName = "USR01"

hRFC = SAP.RfcOpenConnection(RfcConnParams, 5, RfcErrInf)
if hRFC != None:

charBuffer = create_unicode_buffer(512 + 1)

hFuncDesc = SAP.RfcGetFunctionDesc(hRFC, "RFC_READ_TABLE", RfcErrInf)
if hFuncDesc != 0:
hFunc = SAP.RfcCreateFunction(hFuncDesc, RfcErrInf)
if hFunc != 0:

rc = SAP.RfcSetChars(hFunc, "QUERY_TABLE", TableName, \
len(TableName), RfcErrInf)
rc = SAP.RfcSetChars(hFunc, "DELIMITER", "~", 1, RfcErrInf)

if SAP.RfcInvoke(hRFC, hFunc, RfcErrInf) == RFC_OK:

hTable = c_void_p(0)
if SAP.RfcGetTable(hFunc, "DATA", hTable, RfcErrInf) == RFC_OK:

RowCount = c_ulong(0)
rc = SAP.RfcGetRowCount(hTable, RowCount, RfcErrInf)
rc = SAP.RfcMoveToFirstRow(hTable, RfcErrInf)
for i in range(0, RowCount.value):
hRow = SAP.RfcGetCurrentRow(hTable, RfcErrInf)
rc = SAP.RfcGetChars(hRow, "WA", charBuffer, 512, RfcErrInf)
print(charBuffer.value)
if i < RowCount.value:
rc = SAP.RfcMoveToNextRow(hTable, RfcErrInf)

rc = SAP.RfcDestroyFunction(hFunc, RfcErrInf)

rc = SAP.RfcCloseConnection(hRFC, RfcErrInf)

else:
print(RfcErrInf.key)
print(RfcErrInf.message)

del SAP

#-End-------------------------------------------------------------------

2017/04/24: I check the method above with Python release 3.6.1 x64 and with the SAP NetWeaver RFC library 721 PL42 x64 and it works perfect.

16 Comments