Syntax error while loading data from SAP Hana with Python

Question:

I have an issue loading data from SAP HANA via Python.
The code works fine when it is like:

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')

But when I’m trying to add condition like "0COMP_CODE" = ‘B2B’,

so it looks like that:

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' ')

I’m getting SyntaxError: invalid syntax.

What I’m doing wrong?

Thank you.

Asked By: Aksana

||

Answers:

Try to scape single quotes:

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' ')
Answered By: Tiago Gomes
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.