38

Can Python be used to query a SAP database?

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
ilya n.
  • 18,398
  • 15
  • 71
  • 89
  • 1
    SAP as a database engine!? Could you elaborate on that? – middus Sep 23 '09 at 20:47
  • 3
    What I mean is that SAP is a very complicated system but for the purpose of this question I just need to be able to get outputs of ABAP programs (they call output `list`, I think). – ilya n. Sep 24 '09 at 10:07
  • You refer to something called "abap list". Its not easy to get the same output externally. Most of the standard one are not implemented as functions. So you just can't get the output from outside sap without some abap programming. – Igal Serban Sep 29 '09 at 07:17
  • 1
    Calling RFC (Remote Function Calls) with Python works fine: https://github.com/SAP/PyRFC – guettli Nov 29 '19 at 12:10
  • The database connected to an SAP ABAP-based system is always a database which can work standalone. So why should Python communicate with the database through the intermediate SAP system instead of directly? (through ODBC, etc.) The question is really ambiguous and too much general and [will attract opinionated answers](https://stackoverflow.com/help/on-topic) unfortunately. – Sandra Rossi Nov 29 '19 at 15:23

7 Answers7

25

Python SAP RFC module seems inactive - last (insignificant ) commit 2 years ago - but may serve you:

Pysaprfc is a wrapper around SAP librfc (librfc32.dll on Windows, librfccm.so or librfc.so on Linux). It uses the excellent ctypes extension package by Thomas Heller to access librfc and to define SAP compatible datatypes.

Modern SAP versions go the Web Service way - you could build a SAP Web Service and consume it from Python.

With SAP NetWeaver, developers can connect applications and data sources to integrate processes using Web services.

In particular, developers can use one infrastructure to define, implement, and use Web services in an industry standards based way. SAP NetWeaver supports synchronous, asynchronous, stateful and stateless web service models - enabling developers to support different integration scenarios.

sapnwrfc supports this SAP NetWeaver functionality, supersedes the older RFC SDK, and is actively maintained.

Community
  • 1
  • 1
gimel
  • 83,368
  • 10
  • 76
  • 104
18

Python RFC connector is now available as open source: PyRFC

guettli
  • 25,042
  • 81
  • 346
  • 663
bsrdjan
  • 436
  • 3
  • 10
  • Thank you for the link! Do you know whether it is possible to use this with SAP Business 1 as well? I've done a bit of research and it's looking like the SAP NW RFC SDK is not compatible with SAP Business 1. But I haven't found a definitive answer yet. – Tobias Feil Mar 14 '19 at 15:09
  • @TobiasFeil I don't know. But why not ask this as new question here at StackOverflow. Don't forget to use the tag "sap" and then it is likely that there will be an expert who has an answer. – guettli Dec 03 '19 at 13:22
6

SAP now has a Python RFC connector called pyrfc. This supersedes sapnwrfc.

This can be found at: https://github.com/SAP/PyRFC

"The pyrfc Python package provides Python bindings for SAP NetWeaver RFC Library, for a comfortable way of calling ABAP modules from Python and Python modules from ABAP, via SAP Remote Function Call (RFC) protocol."

hansonap
  • 461
  • 1
  • 4
  • 16
  • @bsrdjan Is it possible with PyRFC to automate tasks which a user would normaly perform on the SAP GUI? (for the purpose of test automation) – Wlad Sep 08 '16 at 15:12
  • It depends on the function you want to call, not all functions are written to work with RFC. Its been a while, but digging into the documentation of each function will reveal if it can be called by RFC – hansonap Sep 08 '16 at 21:46
  • If compared with [CCo](http://cco.stschnell.de/) or JCo - does PyRFC cover the same functionality? – Wlad Sep 12 '16 at 10:26
  • Sorry, but I'm unfamiliar with either CCo or JCo. – hansonap Sep 13 '16 at 05:44
  • The purpose is the same, integrating other languages with ABAP via SAP RFC protocol. Some differences in features, like API exposed, are possible. – bsrdjan Mar 01 '17 at 23:35
5

If you're talking about (what used to be named) the SAP Database AKA SapDb, and is now MaxDB (for a while distributed also by MySql Inc, now again by SAP only -- and so named SAP MaxDB), it comes with several Python access modules, documented here.

This is the only meaning I can attach to "SAP as the database engine" -- that you want to access SAP MaxDB. Other answers make different assumptions and (I believe) are also correct... under those different assumptions.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • The original question is not very clear and your interpretation is highly appropriate. SAP installations in the field do not use SAP MaxDB, so I guessed at a somewhat different meaning. – gimel Sep 24 '09 at 04:54
  • Actually, I meant the average SAP installation, forgive my ambiguity. – ilya n. Sep 24 '09 at 10:36
4

Sap is NOT a database server. But with the Python SAP RFC module you can query most table quite easily. It is using some sap unsupported function ( that all the world is using). And this function has some limitation on field size and datatypes.

Igal Serban
  • 10,558
  • 3
  • 35
  • 40
2

As stated above, when you just want to read tables or do RFC or BAPI calls, you can use CPython with the unmaintained Python SAP RFC module or Piers Harding's SAP RFC. The RFC calls to just read a table are RFC_GET_TABLE_ENTRIES or RFC_READ_TABLE, where the former is preferred, but also not released to customers.

For a more official way, you can use SAP's JCO connector with Jython or SAP's .Net Connector with Ironpython; both connectors can be downloaded from SAP's service marketplace and both allow to call the RFC functionality including the two calls listed above.

As also stated above, the way proposed by SAP to access backend functionality is via SAP's vast SOA infrastructure. Here you can use Jython with e.g. the Axis library or Ironpython with Microsofts WCF. Note, that the services provided by SAP obviously won't allow you to access the plain tables, instead you just can call, what a service provides. SAP already delivers about 3.000 services (see the ES Wiki on SDN), and creating your own service is in fact dead simple, once you have your business logic in a remote-enabled function module.

1

Python is one of the most used object-oriented programming languages which is very easy to code and understand.

In order to use Python with SAP, we need to install Python SAP RFC module which is known as PyRFC. One of its available methods is RFC_READ_TABLE which can be called to read data from a table in SAP database.

Also, the PyRFC package provides various bindings which can be utilized to make calls either way. We can use to make calls either from ABAP modules to Python modules or the other way round. One can define equivalent SAP data types which are used in data exchange.

Also, we can create Web Service in Python which can be used for inter-communication. SAP NetWeaver is fully compatible with web services either state full or stateless.

Asish Antony
  • 85
  • 2
  • 11