cx_oracle python 和 select 喜欢 %variable%

cx_oracle python and select Like %variable%

我正在尝试执行基于“tempo”变量的查询,使用 %tempo% none 我在这里找到的解决方案对我的问题有帮助

import cx_oracle
query="""SELECT description, local, point, date  
           FROM tbl_ext_tempo 
          WHERE point like '%' || :0 || '%' 
            AND ROWNUM < 8 
          ORDER BY date DESC
       """
cursor.execute(query, tempo)

异常值:

ORA-01036: illegal variable name/number

您可以定义一个列表(lst),并将当前字符串按格式附加到其中

lst = []
tempo = 'someValue'
query="""
         SELECT description, local, point, "date"
           FROM tbl_ext_tempo
          WHERE point LIKE :0          
            AND ROWNUM < 8
          ORDER BY "date" DESC
       """   
lst.append('%{}%'.format(tempo))
cursor.execute(query,(lst))
print(cursor.fetchall())

date 不能作为保留关键字的列名,可能是引用的 "date"