我需要使用用户提供的输入来更新客户姓名。 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/