首页 > 解决方案 > cx_oracle python 并选择 Like %variable%

问题描述

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

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

标签: pythonoraclecursorcx-oracleexecute

解决方案


您可以定义一个列表(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"引用。


推荐阅读