Forum Stats

  • 3,816,111 Users
  • 2,259,142 Discussions
  • 7,893,390 Comments

Discussions

Bind variables in python

Keith Jamieson
Keith Jamieson Member Posts: 2,815
edited Jun 25, 2009 5:11AM in Python
Hi guys,

is it possible to use bind-variables within a python script. (python 2.5)

If so can you show me an example please

Best Answer

  • 390020
    390020 Member Posts: 446
    edited Jun 17, 2009 12:52PM Answer ✓
    Yes, you can and it's pretty simple (works across all versions since it's a package feature):

    $ ipython
    Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
    Type "copyright", "credits" or "license" for more information.

    In [1]: import cx_Oracle as ora

    In [2]: con = ora.connect('myuser', 'mypass', 'mydb')

    In [3]: cur = con.cursor()

    # you can pass positional arguments
    In [4]: cur.execute("select pay_id from usr_payroll where pay_id<:p1", (5,))

    In [5]: cur.fetchall()
    Out[5]: [(1,), (2,), (3,), (4,)]

    # you can pass named arguments, too
    In [6]: cur.execute("select pay_id from usr_payroll where pay_id<:p1", {'p1':5})

    In [7]: cur.fetchall()
    Out[7]: [(1,), (2,), (3,), (4,)]

Answers

  • 390020
    390020 Member Posts: 446
    edited Jun 17, 2009 12:52PM Answer ✓
    Yes, you can and it's pretty simple (works across all versions since it's a package feature):

    $ ipython
    Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
    Type "copyright", "credits" or "license" for more information.

    In [1]: import cx_Oracle as ora

    In [2]: con = ora.connect('myuser', 'mypass', 'mydb')

    In [3]: cur = con.cursor()

    # you can pass positional arguments
    In [4]: cur.execute("select pay_id from usr_payroll where pay_id<:p1", (5,))

    In [5]: cur.fetchall()
    Out[5]: [(1,), (2,), (3,), (4,)]

    # you can pass named arguments, too
    In [6]: cur.execute("select pay_id from usr_payroll where pay_id<:p1", {'p1':5})

    In [7]: cur.fetchall()
    Out[7]: [(1,), (2,), (3,), (4,)]
  • Keith Jamieson
    Keith Jamieson Member Posts: 2,815
    Thanks for that. I'll pass it on to appropriate people
This discussion has been closed.