IN clause for Oracle Prepared Statement in Python cx_Oracle

前端 未结 3 1597
梦谈多话
梦谈多话 2021-01-06 03:13

I\'d like to use the IN clause with a prepared Oracle statement using cx_Oracle in Python.

E.g. query - select name from employee where id in (\'101\', \'102\'

3条回答
  •  忘掉有多难
    2021-01-06 03:32

    Otra opción es dar formato a una cadena con la consulta.

    import cx_Oracle
    ids = [101, 102, 103]
    ALL_IDS = "('{0}')".format("','".join(map(str, ids)))
    conn = cx_Oracle.connect('username', 'pass', 'schema')
    cursor = conn.cursor()
    
    query = """
    select name from employee where id in ('{}')
    """.format("','".join(map(str, ids)))
    
    results = cursor.execute(query)
    names = [x[0] for x in cursor.description]
    rows = results.fetchall()
    
    

提交回复
热议问题