【问题标题】:cx_oracle python and select Like %variable%cx_oracle python 并选择 Like %variable%
【发布时间】:2021-06-18 05:24:19
【问题描述】:

我正在尝试使用 %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

【问题讨论】:

  • point 列的数据类型是什么,如何在代码中声明tempo 变量?顺便说一句,date 不能作为保留关键字的列名,也许它可能是 "date"
  • point 是 varchar,tempo 是 string,列数据名称是 dt_cri,你说的对。我刚刚改了。我可以从 sql developer 和视图中进行查询,当我想使用变量字符串时,问题就开始了。

标签: python oracle cursor cx-oracle execute


【解决方案1】:

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

【讨论】:

  • 不客气@Jorge Marques,很高兴为您提供帮助。
猜你喜欢
  • 2018-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多