SAP HANA 是SAP 新的內存數據庫:
目前學習python數據處理分析,現在想要連接 SAP HANA 數據庫,
發現目前python已經發布了連接 SAP HANA的庫 pyhddb
介紹詳見https://pypi.python.org/pypi/pyhdb/0.2.3
1、需要安裝pyhdb
pip install pyhdb
2.獲取 Connection 對象
import pyhdb
def get_connection():
conn_obj = pyhdb.connect(
host="10.33.67.12",
port=30015, #多租戶的端口需要准確的如30053,
user="***",
password="***"
)
return conn_obj
3查詢數據
def get_employees(conn,A):
cursor = conn.cursor()
#cursor.execute("select * from XMZX.ZTEST_HANA where ID='a' ") #python官方例子的SQl模式,去掉字段和表的雙引號
cursor.execute("select * from XMZX.ZTEST_HANA where ID='%s' "%(A))# 傳遞參數到
#cursor.execute('select "ID","NAME","ZCLNT" from "XMZX"."ZTEST_HANA" where "ID"=\'a\' ') #HANA生成的SQL需將'轉義
employees = cursor.fetchall()
conn.close()
return employees
if __name__=='__main__':
conn = get_connection()
employees = get_employees(conn,'b')
for employee in employees:
print (employee)
這里只做簡單的連接查詢,其他的可以參照pyhdb的庫來更改
https://blogs.sap.com/2014/04/02/%E5%9C%A8python%E4%B8%AD%E8%BF%9E%E6%8E%A5sap-hana/
這里是用HANA ODBC連接查詢的情況
