python中的Impala查询与变量 - 与%符号混淆?

时间:2017-03-29 22:36:45

标签: sql python-3.x connection impala

我从Python调用了以下Impala查询,工作正常。

reference_ts = "2017-03-11 00:00:00"

q = """

select col_A, col_B, trunc(my_timestamp, 'D') as arrival_wk
  from my_table
    where my_timestamp < "%s"

""" % (reference_ts)

my_df = my_conn.exec_query(q)

然后,如果我稍微修改一下查询:

q1 = """

    select col_A, col_B, trunc(my_timestamp, 'D') as arrival_wk
      from my_table
        where my_timestamp < "%s"
        and col_C like 'AB%'

""" % (reference_ts)

my_df1 = my_conn.exec_query(q1)

我收到以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-81-be1bb3b139a5> in <module>()
     15 
---> 16 """ % (reference_ts)
     17 

ValueError: unsupported format character ''' (0x27) at index 303

我猜Python与%中的col_C like 'AB%'混淆。我想知道是否有办法解决这个问题?谢谢!

0 个答案:

没有答案