SAP HANA Python客户端
hdbcli的Python项目详细描述
简介
python数据库api规范v2.0(pep 249)定义了一组方法,这些方法提供了与实际使用的数据库无关的一致的数据库接口。sap hana的python扩展模块实现了pep 249。安装模块后,可以从python访问和更改sap hana数据库中的信息。
在PEP 249中,自动提交在默认情况下处于关闭状态。在sap hana python驱动程序中,autocommit在默认情况下处于打开状态。
开始
通过pip install hdbcli安装或通过HANA Client Install手动安装
快速启动
- For HANA tenant databases, use the port number 3**NN**13 (where NN is the SAP instance number - e.g. 30013).
- For HANA system databases in a multitenant system, the port number is 3**NN**13.
- For HANA single-tenant databases, the port number is 3**NN**15.
from hdbcli import dbapi conn = dbapi.connect( address="<hostname>", port=3<NN>MM, user="<username>", password="<password>" ) cursor = conn.cursor()
执行不返回结果集的单个语句:
cursor.execute("CREATE TABLE T1 (ID INTEGER PRIMARY KEY, C2 VARCHAR(255))") cursor.close()
使用问号参数绑定将值插入到上面创建的T1表中。参数值以python序列的形式提供,可以是文本值或变量名。本例使用文字值:
sql = 'INSERT INTO T1 (ID, C2) VALUES (?, ?)' cursor = conn.cursor() cursor.execute(sql, (1, 'hello')) # returns True cursor.execute(sql, (2, 'hello again')) # returns True cursor.close()
使用命名参数绑定将值插入T1表。这些值作为python字典提供,本例使用变量名。
sql = 'INSERT INTO T1 (ID, C2) VALUES (:id, :c2)' cursor = conn.cursor() id = 3 c2 = "goodbye" cursor.execute(sql, {"id": id, "c2": c2}) # returns True cursor.close()
在结果集的行上循环。
sql = 'SELECT * FROM T1' cursor = conn.cursor() cursor.execute(sql) for row in cursor: print(row)
帮助
有关使用sap hana python客户端开发的详细信息,请参见SAP HANA Client Interface Programming Reference。
许可证
hana python客户端是通过SAP Developer License Agreement提供的。
通过使用本软件,您同意将以下文本纳入开发人员协议的条款中:
If you are an existing SAP customer for On Premise software, your use of this current software is also covered by the terms of your software license agreement with SAP, including the Use Rights, the current version of which can be found at: https://www.sap.com/about/agreements/product-use-and-support-terms.html?tag=agreements:product-use-support-terms/on-premise-software/software-use-rights