Invalid syntax when using the cursor.execute in python ( sagemaker notebook)

Question:

select *
FROM HEVOPROD_DB.HEVOPROD_SCH.PRDNEW_METERING
where REQUESTED_SERVICE = ‘["dsp"]’
and longitude is not null ;

  1. The above query works fine when i query this in snowflake while using this query in sagemaker notebook/ jupyter notebook it just fails stating invalid syntax

  2. important point here is REQUESTED_SERVICE = ‘["dsp"]’ , the value is enclosed in single quote

  3. this is how i’am using it , but i get a synatax error

    try:
        cs.execute("select * FROM HEVOPROD_DB.HEVOPROD_SCH.PRDNEW_METERING where REQUESTED_SERVICE = '["dsp"]' and storeid is not null and latitude is not null and longitude is not null ")
        prev = time() 
        for df in cs.fetch_pandas_batches():
            print(time() - prev)
            print(df.shape)
            temp_all_dfs.append(df)
            prev = time()
    finally:
        cs.close()

    ctx.close()
Asked By: lifo

||

Answers:

In this line:

cs.execute("select * FROM HEVOPROD_DB.HEVOPROD_SCH.PRDNEW_METERING where REQUESTED_SERVICE = '["dsp"]' and storeid is not null and latitude is not null and longitude is not null ")

You delineate the query text in double quoted strings. Any double quote within double quoted strings must be escaped.

cs.execute("select * FROM HEVOPROD_DB.HEVOPROD_SCH.PRDNEW_METERING where REQUESTED_SERVICE = '["dsp"]' and storeid is not null and latitude is not null and longitude is not null ")

A more elegant solution might be to remove the literal value from the query altogether and use a parameter instead

cs.execute(
  "select * FROM HEVOPROD_DB.HEVOPROD_SCH.PRDNEW_METERING where REQUESTED_SERVICE = %s and storeid is not null and latitude is not null and longitude is not null ",
  '["dsp"]',
)
Answered By: erik258