python-3.x - 需要动态执行SQL查询

标签 python-3.x oracle oracle11g pycharm

我需要使用用户提供的输入来更新客户姓名。 Original_name 需要由 new_name 更新

cur.execute(''' 
CREATE TABLE customer (customerId int,customerName varchar(20),telephoneNo int,
                    addressId varchar(20), customerType varchar(20),
                    discount int, memCardType varchar(20))''')

我正在尝试执行此代码,但出现错误。

    original_name=input("Enter name to be modified: ")
    new_name=input("Enter new name of the customer: ")
    cur.execute("UPDATE customer SET customerName=new_name WHERE  customerName=original_name   ")
    print("Customer name changes successfully.")

错误:

 cur.execute("UPDATE customer SET customerName=new_name WHERE  
customerName=original_name   ")
cx_Oracle.DatabaseError: ORA-00904: "ORIGINAL_NAME": invalid identifier

最佳答案

我想你使用 SQL 游标,所以我会这样做:

original_name=input("Enter name to be modified: ")
new_name=input("Enter new name of the customer: ")
cur.execute("UPDATE customer SET customerName=:new_name WHERE  customerName=:cust_name",{'cust_name': original_name, 'new_name' : new_name})
print("Customer name changes successfully.")

这个概念很简单。 :new_name:cust_name 是绑定(bind)参数,事实上,您告诉函数您将将此参数添加为变量,然后在

{'cust_name': original_name, 'new_name' : new_name}

您只需用变量填充这些绑定(bind)参数即可。

关于python-3.x - 需要动态执行SQL查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50772140/

相关文章:

sql - 如何在Oracle中找到负最大值和正最小值?

oracle - 行锁定的原子性

python - 字符串操作执行不正确

linux - 如何在 linux 中为 python 3 安装 poppler?

sql - PL/SQL block 问题 : No data found error

java - 在 ibatis 中是否可以在同一个插入查询中有多个 selectKey 子句?

java - JPA Criteria Query with predicate 比较注释@Convert的字段在执行时得到ORA-00932

linux - oracle执行sqlplus时报错: "SP2-1503: Unable to initialize Oracle call interface"

python-3.x - 线程和异步: Task was destroyed but it is pending

python - python 3中的对齐、间距