简体   繁体   中英

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.

: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

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!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM