从python运行SAP Hana查询时的ProgrammingError

时间:2017-12-14 19:17:17

标签: python hana

我使用sqlalchemy

连接到SAP Hana
_dsn='dsn'
conn_str = 'mssql+pyodbc://@{dsn}'.format(dsn=_dsn)
engine = sa.create_engine((conn_str)

然后我有一个使用run()函数的python文件。

def run(engine):
    """
    TBD
    """

    query = 
          """
        SELECT
           "CalendarDay",
           "Score"
        FROM "Table"
        WHERE  "PEvent" = ''H'' OR "PEvent" = ''T''
        ORDER BY "CalendarDay" DESC, "Score" ASC
            """
    result = pd.read_sql(query, engine)
    return result

以下是我收到的2条错误消息:

ProgrammingError: ('42000', '[42000] [SAP AG][LIBODBCHDB SO][HDBODBC] Syntax error or access violation;257 sql syntax error: incorrect syntax near "H": line 5 col 25 (at pos 328) (257) (SQLExecDirectW)')

ProgrammingError: (pyodbc.ProgrammingError) ('42000', '[42000] [SAP AG][LIBODBCHDB SO][HDBODBC] Syntax error or access violation;257 sql syntax error: incorrect syntax near "H": line 15 col 25 (at pos 328) (257) (SQLExecDirectW)') [SQL: '\n SELECT\n "CalendarDay",\n "Score",\n FROM "Table"\n WHERE "PEVENT" = \'\'H\'\' OR "PEVENT" = \'\'T\'\'\n ORDER BY "CalendarDay" DESC, "Score" ASC\n']

我的猜测是H和T旁边的那些反斜杠正在创造这个问题。 由于我们在查询的开头有一个'在'前面''H关闭字符串。

不幸的是,我找不到编写我的python代码的正确方法,因此SAP Hana可以阅读它。

1 个答案:

答案 0 :(得分:0)

我找到了一种处理此问题的不同连接方法。

conn = pyodbc.connect(DSN='dsn')
cursor = conn.cursor()
cursor.execute(query)
rows = cursor.fetchall()

此连接在查询中处理字符串问题。