Tiberius
Tiberius

Reputation: 145

Displaying results from an Oracle query based on user input

I am trying to query the records for a specific ID in an Oracle table based on what the user inputs.

Here is my code:

import cx_Oracle
con = cx_Oracle.connect('dbuser/dbpassword@oracle_host/service_ID')
cur = con.cursor()
id_number = raw_input('What is the ID Number?')
cur.execute('select id, info from oracle_table_name where id=:id_number')
for result in cur:
    print "test", result
cur.close()
con.close()

The following error pops up: cx_Oracle.DatabaseError: ORA-01008: not all variables bound

When I remove the user input and the variable substitution and run the query, everything works fine.

Upvotes: 1

Views: 1616

Answers (2)

Tiberius
Tiberius

Reputation: 145

I assigned a name to the user_value:

user_value = raw_input('What is the ID Number?')

And then referenced it in the execute statement:

cur.execute(query, {'id': (user_value)})

Thanks to Radoslaw-Roszkowiak for the assist!!

Upvotes: 1

Radosław Roszkowiak
Radosław Roszkowiak

Reputation: 6881

:id_number in your SQL is a parameter (variable). You need to provide its value. execute method accepts parameters as the second argument.

Example: query = "select * from some_table where col=:my_param" cursor.execute(query, {'my_param': 5})

Check the documentation at http://cx-oracle.readthedocs.org/en/latest/cursor.html#Cursor.execute

Upvotes: 2

Related Questions