Jolly23
V2EX  ›  问与答

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

  •  
  •   Jolly23 · Aug 10, 2016 · 3467 views
    This topic created in 3615 days ago, the information mentioned may be changed or developed.

    众所周知, 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 replies    2016-08-10 22:26:37 +08:00
    hcymk2
        1
    hcymk2  
       Aug 10, 2016
    cursor.execute(sql_sentence, ('%爸爸%'))
    这样?
    wowpanda
        2
    wowpanda  
       Aug 10, 2016 via Android
    可以用字符串格式化啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1031 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 19:11 · PVG 03:11 · LAX 12:11 · JFK 15:11
    ♥ Do have faith in what you're doing.