使用 Python 从 SAP Hana 加载数据时出现语法错误

Syntax error while loading data from SAP Hana with Python

我在通过 Python 从 SAP HANA 加载数据时遇到问题。 代码在以下情况下工作正常:

connection = pyhdb.connect(|||credentials|||)
cursor = connection.cursor()
cursor.execute('SELECT "0COMP_CODE", "0VENDOR", "0REF_DOC_NO", "0BBP_INV_ID" FROM  "_SYS_BIC"."system-local.bw.bw2hana/ZFGL01MY" WHERE CAST( "0PSTNG_DATE" AS date) >= ADD_MONTHS( NEXT_DAY( LAST_DAY( CURRENT_DATE)), -2) and "0VENDOR" IS NOT NULL')

但是当我尝试添加诸如“0COMP_CODE”= 'B2B'、

之类的条件时

看起来像这样:

Connection = pyhdb.connect(|||credentials|||)
cursor = connection.cursor()
cursor.execute('SELECT "0COMP_CODE", "0VENDOR", "0REF_DOC_NO", "0BBP_INV_ID" FROM  "_SYS_BIC"."system-local.bw.bw2hana/ZFGL01MY" WHERE CAST( "0PSTNG_DATE" AS date) >= ADD_MONTHS( NEXT_DAY( LAST_DAY( CURRENT_DATE)), -2) and "0VENDOR" IS NOT NULL AND "0COMP_CODE" = 'B2B' ')

我得到 SyntaxError: invalid syntax.

我做错了什么?

谢谢。

尝试转义单引号:

Connection = pyhdb.connect(|||credentials|||)
cursor = connection.cursor()
cursor.execute('SELECT "0COMP_CODE", "0VENDOR", "0REF_DOC_NO", "0BBP_INV_ID" FROM  "_SYS_BIC"."system-local.bw.bw2hana/ZFGL01MY" WHERE CAST( "0PSTNG_DATE" AS date) >= ADD_MONTHS( NEXT_DAY( LAST_DAY( CURRENT_DATE)), -2) and "0VENDOR" IS NOT NULL AND "0COMP_CODE" = \'B2B\' ')