Home > Mobile >  comment SQL query with variables in python
comment SQL query with variables in python

Time:05-14

I'm working with Cloud Function. I have the following query working correctly:

# this is working
q = """ SELECT col1, col2
        FROM `my_table` 
        WHERE col1 = {} AND col2 = '{}'""".format(var1, var2)

however when trying to add a comment I have a Keyerror:

# this is not working
q = """ /* "{'query': 'some_name' */ 
        SELECT col1, col2
        FROM `my_table` 
        WHERE col1 = {} AND col2 = '{}'""".format(var1, var2)

The query is working with the comment if I'm removing the variables:

# this is working
q = """ /* "{'query': 'some_name' */ 
        SELECT col1, col2
        FROM `my_table` """

I'm using the following function to run my query:

def run_query(q):
    client = bigquery.Client()
    df_result = client.query(q).to_dataframe()
    return df_result

Why do I get this error? How should I comment in this context?

CodePudding user response:

As stated in the comments by @Jaytiger:

you need to escape a curly brace in your comment by adding one more curly brace like this :

/* "{{'query': 'some_name' */
  • Related