Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

    MEDevel.com: Open-source for Healthcare and Education

    Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

    Visit Medevel

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

>Solution :

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"]',
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading