def connect(self): '''Connects to the SAP system.''' params = 'ASHOST=%s SYSNR=%s CLIENT=%s USER=%s PASSWD=%s' % (self.host, self.sysnr, self.client, self.user, self.password) try: self.sap = pysap.Rfc_connection(conn_string = params) self.sap.open() except pysap.BaseSapRfcError, se: # Put in the error message the connection string without the # password. connNoPasswd = params[:params.index('PASSWD')] + 'PASSWD=********' raise SapError(SAP_CONNECT_ERROR % (connNoPasswd, str(se)))
# transparent table TCURR. We'd like our output to be: date, forreign currency (ISO code), # local currency (ISO code), exchange rate. The problem is SAP stores dates in TCURR in so-called # inverse format which is not suitable for display. Therefore we'll create additional table # having same fields as TCURR with an extra date field added. In this field we'll store the # converted date. We'll then sort the new table by date in ascending order and print it. import pysap def inv_to_date(inv_date): # Small function to convert SAPs inverse date to 'normal' date (internal format) return str(99999999 - int(inv_date)) # Change next line to be able to connect to your SAP system sap_conn = pysap.Rfc_connection(conn_file='sapconn.ini', conn_name='my_connection') sap_conn.open() # Get field description for structure TCURR (foreign currency exchange rates) from DDIC tcurr_fld_lst_ext = sap_conn.get_fieldlist('TCURR') # Append additional field tcurr_fld_lst_ext.append(('datum', 'D')) # Read 20 entries from TCURR for AUD and exchange rate type 'M' itab_tcurr = sap_conn.read_table( 'TCURR', options=["fcurr eq 'AUD' and ", "kurst eq 'M'"], max_rows=20) # Create new internal table with extended structure - new table has same fields as itab_tcurr plus # additional field 'datum' to store dates itab_tcurr_ext = pysap.create_table('itab1', tcurr_fld_lst_ext) #Fill created table with entries from SAP for l in itab_tcurr: l1 = itab_tcurr_ext.struc() l1.copy_corresp_from(l)
# This examples shows how to perform remote function calls # Connection to SAP R/3 system is needed for this example import pysap import sys import ctypes # Modify next line to suit your connection details sap_conn = pysap.Rfc_connection(conn_file='sapconn.ini') sap_conn.open() # Create remote function interface func = sap_conn.get_interface('RFC_GET_TABLE_ENTRIES') # Print its description print func.desc # Fill the arguments needed func['TABLE_NAME'] = 'T001' func['MAX_ENTRIES'] = 20 # Perform function call # Only the arguments specified will be passed to SAP. This reduces bandwith. # Note: Trying to access other parameters defined by 'RFC_GET_TABLE_ENTRIES' # but not sent to SAP will raise SapRfcError try: rc = func('TABLE_NAME', 'MAX_ENTRIES', 'ENTRIES') except pysap.SapRfcError, desc: print "Error invoking 'RFC_GET_TABLE_ENTRIES': %s" % desc else:
sys.exit(1) config = {} execfile(sys.argv[1], globals(), config) if not config.has_key('db_name'): print 'Error: "db_name" missing' sys.exit(1) if not config.has_key('connection_string') and not config.has_key( 'connection_file'): print 'Error: specify either "connection_string" or "connection_file"' sys.exit(1) if config.has_key('connection_string'): conn = pysap.Rfc_connection(conn_string=config['connection_string']) else: if not config.has_key('connection_name'): config['connection_name'] = '' conn = pysap.Rfc_connection(conn_file=config['connection_file'], conn_name=config['connection_name']) try: conn.open() except pysap.SapRfcError, e: print 'Error opening connection: "%s"' % e sys.exit(1) try: db = pysap.Storage(config['db_name']) except Exception, e: print 'Error opening database: "%s"' % e
# This is an example of how to use the Select object of the pyabap module. # Connection to a SAP R/3 system is needed for this example. # # pyabap.py is currently in its testing stage and is there to demonstrate the usage # of exec_abap method which was added to RfcConnection in release 0.99.0. In future it # migth become part of standard pysaprfc package. import pysap import pyabap import time import sys conn = 'LCHECK=1 ASHOST=localhost CLIENT=000 SYSNR=17 USER=developer PASSWD=***' sap_conn = pysap.Rfc_connection(conn) try: sap_conn.open() except pysap.SapRfcError, desc: print desc sys.exit(1) itab = sap_conn.get_table('TCURR') # Uncomment the next line if you want to just fetch a subset of fields #itab=pysap.ItTable('itab',sap_conn.get_structure('TCURR','FCURR','TCURR','KURST','UKURS')) s = pyabap.Select(sap_conn, 'TCURR', itab, ['fcurr'], from_line=10, to_line=20) s.append_cond('fcurr', 'I', '=', 'EUR') try: t = time.time() rez = s() t = time.time() - t except pysap.SapRfcError, desc: print desc else: