根据用户输入显示来自 Oracle 查询的结果
Displaying results from an Oracle query based on user input
我正在尝试根据用户输入的内容在 Oracle table 中查询特定 ID 的记录。
这是我的代码:
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()
弹出如下错误:cx_Oracle.DatabaseError: ORA-01008: not all variables bound
当我删除用户输入和变量替换以及 运行 查询时,一切正常。
:id_number
在你的 SQL 是一个参数(变量)。你需要提供它的价值。
execute
方法接受参数作为第二个参数。
示例:
query = "select * from some_table where col=:my_param"
cursor.execute(query, {'my_param': 5})
在 http://cx-oracle.readthedocs.org/en/latest/cursor.html#Cursor.execute
查看文档
我为 user_value 指定了一个名称:
user_value = raw_input('What is the ID Number?')
然后在执行语句中引用它:
cur.execute(query, {'id': (user_value)})
感谢Radoslaw-Roszkowiak的协助!!
我正在尝试根据用户输入的内容在 Oracle table 中查询特定 ID 的记录。
这是我的代码:
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()
弹出如下错误:cx_Oracle.DatabaseError: ORA-01008: not all variables bound
当我删除用户输入和变量替换以及 运行 查询时,一切正常。
:id_number
在你的 SQL 是一个参数(变量)。你需要提供它的价值。
execute
方法接受参数作为第二个参数。
示例:
query = "select * from some_table where col=:my_param"
cursor.execute(query, {'my_param': 5})
在 http://cx-oracle.readthedocs.org/en/latest/cursor.html#Cursor.execute
查看文档我为 user_value 指定了一个名称:
user_value = raw_input('What is the ID Number?')
然后在执行语句中引用它:
cur.execute(query, {'id': (user_value)})
感谢Radoslaw-Roszkowiak的协助!!