2

I'm trying to run the following query.

i=['qwerty12345']
%python
  query='''select 
column1,column2
from table
where hit_id='{hit_id}'
and device_id='{'{AB14233Q-2E60}'}'
'''.format(hit_id=i)
result= sqlContext.sql(query)

But I'm getting ValueError: unexpected '{' in field name my device_id is '{AB14233Q-2E60}' What am I missing here? I really appreciate any help you can provide.

0

4 Answers 4

2

The format method takes every { and } as an attempt to write a placeholder.

When that is not the case, you have to escape them like this {{, }}.

So, you would rewrite your code to this:

i=['qwerty12345']
%python
query='''select 
column1,column2
from table
where hit_id='{hit_id}'
and device_id='{{'{{AB14233Q-2E60}}'}}'
'''.format(hit_id=i)
result= sqlContext.sql(query) 
Sign up to request clarification or add additional context in comments.

Comments

2

Just in case this helps anyone else: one of my format placeholders accidentally had an incorrectly closed bracket, so check that your brackets are symmetrical.

Incorrect: {item1} {item2} {item3)

------------------------------------^

Correct: {item1} {item2} {item3}

Comments

1

You would need an escape sequence to solve this. Try adding double curly braces like this {{ }} :

query= '''select 
    column1,column2
    from table
    where hit_id='{hit_id}'
    and device_id='{{'{{AB14233Q-2E60}}'}}'
    '''.format(hit_id=i)

This should hopefully do the trick

Comments

0

You have to escape the quotes in your device_id

'''select 
column1,column2
from table
where hit_id='{hit_id}'
and device_id='{\'{AB14233Q-2E60}\'}'
'''

1 Comment

I'm still getting the same error. My device_id is '{AB14233Q-2E60}'. do we need two { in the query? @AlexisG

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.