V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Jolly23
V2EX  ›  问与答

关于 cx_Oracle 执行语句上的一些问题

  •  
  •   Jolly23 · 2016-08-10 16:59:58 +08:00 · 2656 次点击
    这是一个创建于 2787 天前的主题,其中的信息可能已经有所发展或是发生改变。

    众所周知, cx_Oracle 在运行 sql 语句时,可以这么写:

    连接过程忽略,只看执行

    sql = '''select phone_num from address_book where name = :1'''
    
    cursor.execute(sql_sentence, ('爸爸'))
    

    这样, sql 中的:1 位置就被替换成了 ‘爸爸’

    问题来了,我要把句子这么写:

    sql = '''select phone_num from address_book where name like %:1% '''
    

    只要包含关键字就取出,可是 ------> %:1% <----- 这么写是错误的,-----> '%:1%' <----- 这么写也是错误的 请问涉及到 like 的句子,怎么写能使用上 cx_Oracle 的参数替换? cursor.execute(sql_sentence, ('爸爸'))

    2 条回复    2016-08-10 22:26:37 +08:00
    hcymk2
        1
    hcymk2  
       2016-08-10 18:02:25 +08:00
    cursor.execute(sql_sentence, ('%爸爸%'))
    这样?
    wowpanda
        2
    wowpanda  
       2016-08-10 22:26:37 +08:00 via Android
    可以用字符串格式化啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3369 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 00:43 · PVG 08:43 · LAX 17:43 · JFK 20:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.