![](/img/trans.png)
[英]How do I found out the Host/Port when connecting to SAP HANA using python PyHDB
[英]I am unable to catch the exception created when querying a non existent SAP HANA table using pyodbc/python
如果我查詢一個最終不存在的表,我想創建一個新表。 我想通過捕獲生成的異常來做到這一點,但盡管指定了在我的 except 語句中彈出的錯誤,但我無法做到這一點。
我下面的每個 try 塊最后都會導致兩個異常。
import pyodbc as py
qry = \
"""
SELECT *
FROM NON_EXISTENT_TABLE
"""
cmd = \
"""
CREATE TABLE SOME_TABLE(VAR1 VARCHAR(10),
VAR2 VARCHAR(50), VAR3 VARCHAR(5), VAR4 VARCHAR(6));
"""
try:
df = pd.read_sql_query(qry, py.connect('DSN=SOMEDSN; Trusted_Connection = Yes'))
except py.ProgrammingError:
py.connect('DSN=SOMEDSN; Trusted_Connection=Yes').cursor().execute(cmd)
try:
df = pd.read_sql_query(qry, py.connect('DSN=SOMEDSN; Trusted_Connection = Yes'))
except py.DatabaseError:
py.connect('DSN=SOMEDSN; Trusted_Connection=Yes').cursor().execute(cmd)
這是我得到的錯誤
ProgrammingError: ('42S02', '[42S02] [SAP AG][LIBODBCHDB DLL][HDBODBC] Base table or view not found;259 invalid table name: Could not find table/view NON_EXISTENT_TABLE in schema SOMESCHEMA: line 3 col 15 (at pos 25) (259) (SQLExecDirectW)')
The above exception was the direct cause of the following exception:
DatabaseError: Execution failed on sql '
SELECT *
FROM SOMESCHEMA.NON_EXISTENT_TABLE
': ('42S02', '[42S02] [SAP AG][LIBODBCHDB DLL][HDBODBC] Base table or view not found;259 invalid table name: Could not find table/view NON_EXISTENT_TABLE in schema SOMESCHEMA: line 3 col 15 (at pos 25) (259) (SQLExecDirectW)')
我無法就具體的 Python 問題提供答案。 但是,了解 SAP為 SAP HANA 提供高級 Python 客戶端可能會對您有所幫助,這使生活變得更加輕松,尤其是在使用 Pandas 數據幀時。
使用hana-ml 客戶端,您可以使用 function has_table
來檢查表是否存在。 根據結果,您可以使用 function create_table
創建一個新表。 最后,您可以使用HANA Dataframe查詢數據並將結果collect
為 Pandas dataframe。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.