Python cx_Oracle bind variables

后端 未结 2 609
忘了有多久
忘了有多久 2020-11-30 04:07

I am a Python newbie, I am having troubles in the use of bind variables. If I execute the code below everything works fine.

bind= {\"var\" : \"ciao\"}
sql =          


        
2条回答
  •  孤街浪徒
    2020-11-30 04:54

    You are misusing the binding.

    There are three different ways of binding variables with cx_Oracle as one can see here :

    1) by passing a tuple to a SQL statement with numbered variables :

    sql = "select * from sometable where somefield = :1 and otherfield = :2"
    cur.execute(sql, (aValue, anotherValue))
    

    2) By passing keyword arguments to a SQL statement with named variables :

    sql = "select * from sometable where somefield = :myField and otherfield = :anotherOne"
    cur.execute(sql, myField=aValue, anotherOne=anotherValue)
    

    3) By passing a dictionary to a SQL statement with named variables :

    sql = "select * from sometable where somefield = :myField and otherfield = :anotherOne"
    cur.execute(sql, {"myField":aValue, "anotherOne":anotherValue})
    

    Remarks

    Why does your code work then ?

    Let's try to understand what happens here :

    bind= {"var" : "ciao"}
    sql = "select * from sometable where somefield = :bind and otherfield = :bind"
    cur.execute(sql,(bind["var"], bind["var"]))
    

    Oracle will understand that it expects one variable. This is a named variable, linked by name bind. You should then give a parameter as a named parameter like this :

    cur.execute(sql, bind="ciao")
    

    Or using a dictionary, like that :

    cur.execute(sql, {bind:"ciao"})
    

    However, as cx_Oracle receives a tuple instead, it fallbacks in a binding by number, as if your SQL statement was :

    sql = "select * from sometable where somefield = :1 and otherfield = :2"
    

    And as you are passing bind['var'] twice, which is just the string "ciao". It is mapping the two tuple items to the numbered variables :

    cur.execute(sql, ("ciao", "ciao"))
    

    That runs by chance but the code is very misleading.

    Tuple with a single value to bind

    Also note that the first option requires a tuple. But if you have a single value to bind, you can use this notation to create a tuple of a single value :

    sql = "select * from sometable where somefield = :1"
    cur.execute(sql, (aValue,))
    

    [EDIT] : Thanks to @tyler-christian for mentioning that passing a dict was supported by cx_Oracle.

提交回复
热议问题