Query SAP database from Python? [closed]
Asked Answered
K

7

38

Can Python be used to query a SAP database?

Kensell answered 23/9, 2009 at 15:55 Comment(5)
SAP as a database engine!? Could you elaborate on that?Jonette
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).Kensell
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.Nummulite
Calling RFC (Remote Function Calls) with Python works fine: github.com/SAP/PyRFCBindweed
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 unfortunately.Torrence
U
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.

Upthrust answered 23/9, 2009 at 16:24 Comment(3)
+1: Python web services are very easy to write using urllib2.Diatribe
Great, web services are the way to go!Kensell
This is clearly the technical answer to the question - but if all you're doing is writing queries, why? SQ01 & SQVI provide query access to the database.Degraw
A
18

Python RFC connector is now available as open source: PyRFC

Autoradiograph answered 29/10, 2012 at 18:14 Comment(2)
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.Determinative
@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.Bindweed
M
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."

Moonseed answered 8/7, 2015 at 18:19 Comment(5)
@Autoradiograph Is it possible with PyRFC to automate tasks which a user would normaly perform on the SAP GUI? (for the purpose of test automation)Rudolf
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 RFCMoonseed
If compared with CCo or JCo - does PyRFC cover the same functionality?Rudolf
Sorry, but I'm unfamiliar with either CCo or JCo.Moonseed
The purpose is the same, integrating other languages with ABAP via SAP RFC protocol. Some differences in features, like API exposed, are possible.Autoradiograph
A
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.

Anaphora answered 23/9, 2009 at 20:45 Comment(2)
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.Upthrust
Actually, I meant the average SAP installation, forgive my ambiguity.Kensell
N
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.

Nummulite answered 23/9, 2009 at 18:49 Comment(1)
well you could use it as a database server - albeit a very expensive one :PWindsail
A
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.

Algae answered 6/10, 2009 at 9:35 Comment(0)
D
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.

Deferential answered 6/12, 2019 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.