Python cx_Oracle bind variables

后端 未结 2 603
忘了有多久
忘了有多久 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:43

    @ffarquest says that using a dictionary isn't supported by cx_Oracle but it is in fact, @giovanni-de-ciantis was just using it incorrectly.


    named_params = {'dept_id':50, 'sal':1000}
    query1 = cursor.execute(
        'SELECT * FROM employees WHERE department_id=:dept_id AND salary>:sal',
        named_params
    )
    

    OR

    query2 = cursor.execute(
        'SELECT * FROM employees WHERE department_id=:dept_id AND salary>:sal',
        dept_id=50,
        sal=1000
    )
    

    In the given example, I believe the second reference to :bind would need to be replaced with something different because it isn't being done in sequential order. Also, renamed the variable bind to get rid of confusion.

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

    This article is from 2007 showing that you can use a dictionary: http://www.oracle.com/technetwork/articles/dsl/prez-python-queries-101587.html

提交回复
热议问题