Skip to main content
 首页 » 编程设计

python之sapnwrfc 调用总是返回 None/NULL

2025年05月04日9thcjp

我知道连接 python 和 SAP 的最新库是 PyRFC ,我正在使用 Windows 开发一个 django 应用程序,当我尝试安装与我的系统对应的 pyrfc-1.9.3-py2.7-win32.egg 时,它给了我错误导入库,显示错误是因为缺少 pyrfc 导入的模块,我遵循了整个 README 文档,但我不知道如何使用这个库。

我决定使用 sapnwrfc相反,所以我下载了源代码并用 MinGW 编译它,它安装得很好,我现在可以与 SAP 建立连接,但是调用 RFC 函数时出错。

这是我的代码

def results(request): 
 
    sapnwrfc.base.config_location = BASE_DIR+'\\sap.yml' 
    sapnwrfc.base.load_config() 
 
    try: 
        conn = sapnwrfc.base.rfc_connect() 
        fd = conn.discover("ZRFC_TEST_TODO") 
        f = fd.create_function_call() 
        f.QUERY_TABLE("IT_SUCS") # ERROR IN THIS LINE 
        f.ROWCOUNT(50) 
        f.OPTIONS=([{'BUKRS': "TVN"}]) 
        f.invoke() 
 
        d = f.DATA 
        todo = {'results': d} 
 
        conn.close() 
 
    except sapnwrfc.RFCCommunicationError as e: 
        todo = {'error':e} 
    return render_to_response(json.dumps(todo), content_type='application/json') 

错误是

NoneType object is not callable

如果我把它改成

f.QUERY_TABLE="IT_SUCS" 
f.ROWCOUNT=50 
f.OPTIONS=[{'BUKRS': "TVN"}] 
f.invoke() 

然后错误消失,但 f 始终为 null。

我需要从 SAP 的 RFC 中获取一些表格,有解决这个问题的想法吗?还有另一种方法吗?也许是另一个图书馆?

更新 经过测试和调试后,我认为 fd 变量未正确初始化,因为当我尝试查看 fd 属性时,python 停止并显示错误

TypeError: 'NoneType' object is not callable

请您参考如下方法:

有点晚了...

def get_table(): 
    QUERY_TABLE = 'MY_TABLE' 
    Fields = ['MY_FIELD'] 
    FIELDS=[{'FIELDNAME':x} for x in Fields] 
    get_data = conn.call("RFC_READ_TABLE", DELIMITER='|', FIELDS=FIELDS, QUERY_TABLE=QUERY_TABLE, ) 
    get_data = get_data['something in get_data'] 
    # here format your data, for x in ...