Python connect to SAP HANA using dbapi
Below is an sample Python script using dbapi to connect SAP HANA.
#import the database connect api
import dbapi
#argument, please replace userNamen and passWord
serverAdress='fandezhi.com'
serverPort=35015
userName='JLIU'
passWord='Password'
#connect to hana database
conn=dbapi.connect(serverAdress,serverPort,userName,passWord)
#query
query="select CUST_ID, CUST_CLASSF, NAME1, ADDRESS, CITY, REGION_ID, ZIP, CUST_TIMESTAMP from JLIU.TBL_CUSTOMER"
cursor=conn.cursor()
try:
ret=cursor.execute(query)
ret=cursor.fetchall()
for row in ret:
for col in row:
print col,
print
except Exception,ex:
print ex
#insert data
query="insert into JLIU.TBL_CUSTOMER values('DT04','OR','Safer Airlines','99 Berlin Ave.','Spokane','3','90909','2008-03-27 12:00:00.0 AM')"
try:
ret=cursor.execute(query)
except Exception,ex:
print ex
#close connection and cursor
conn.close()
cursor.close()
Save above code as my1st.py at C:\Program Files\sap\hdbclient\Python then you can test with it.
**Python connect to SAP HANA using dbapi**
Below is an sample Python script using dbapi to connect SAP HANA.
````
#import the database connect api
import dbapi
#argument, please replace userNamen and passWord
serverAdress='fandezhi.com'
serverPort=35015
userName='JLIU'
passWord='Password'
#connect to hana database
conn=dbapi.connect(serverAdress,serverPort,userName,passWord)
#query
query="select CUST_ID, CUST_CLASSF, NAME1, ADDRESS, CITY, REGION_ID, ZIP, CUST_TIMESTAMP from JLIU.TBL_CUSTOMER"
cursor=conn.cursor()
try:
ret=cursor.execute(query)
ret=cursor.fetchall()
for row in ret:
for col in row:
print col,
print
except Exception,ex:
print ex
#insert data
query="insert into JLIU.TBL_CUSTOMER values('DT04','OR','Safer Airlines','99 Berlin Ave.','Spokane','3','90909','2008-03-27 12:00:00.0 AM')"
try:
ret=cursor.execute(query)
except Exception,ex:
print ex
#close connection and cursor
conn.close()
cursor.close()
````
Save above code as my1st.py at C:\Program Files\sap\hdbclient\Python then you can test with it.
![578094406d406.jpg](serve/attachment&path=578094406d406.jpg)
Perfect is not enough - must be irreplaceable!
edited Jul 19 '16 at 2:36 am