使用 python 更新行
updating rows using python
所以在这里我想写一个python代码来更新一个table。就像我创建了一个名为 customers 的 table,其中包含一些列。我想更新从用户那里获取输入的列,如果用户没有提供任何输入,我希望它自己采用以前的值。请帮助我,我是 python.
的新手
dummy=[]
user_input=int(input("Enter the identification number you want to change:"))
First_name=input("Enter the First name:")
if len(First_name)!=0:
dummy+="Firstname="+First_name
cursor.execute("update customer4 set Firstname=? where identification=?")
values=(dummy,user_input)
conn.commit
Last_name=input("Enter the last name:")
if len(Last_name)!=0:
if len(First_name)!=0:
dummy+="Firstname"+First_name, "Lastname"+Last_name
cursor.execute("update customer4 set Firstname=?, Lastname=? where identification=?")
values=(dummy,user_input)
conn.commit
else:
dummy+="Lastname"+Last_name
cursor.execute("update customer4 set Lastname=? where identification=?")
values=(dummy,user_input)
conn.commit
print(cursor.rowcount,"record affected")
这是我的 python 代码。在我学习的过程中帮助我 python.
您的 execute() 行的语法错误。使用 ”?”作为占位符是正确的方法,但您的值需要像这样插入:
cursor.execute("update customer4 set Firstname=? where identification=?", (dummy,user_input))
user_input=int(input("Enter the identification number you want to change:"))
query = "SELECT identification FROM CUSTOMER4 WHERE identification=?"
cursor.execute(query, (user_input,))
data = cursor.fetchall()
conn.commit()
if len(data) == 0:
print("Does not exist")
else:
print("Enter the first name:")
a=input()
print("Enter the last name:")
b=input()
print("Enter the email id:")
c=input()
print("Enter phone no:")
d=input()
if len(a)==0:
a=("SELECT Firstname from CUSTOMER4 where identification=?",user_input)
conn.commit()
if len(b)==0:
b=("SELECT Lastname from CUSTOMER4 where identification=?",user_input)
conn.commit()
if len(c)==0:
c=("SELECT Email from CUSTOMER4 where identification=?",user_input)
conn.commit()
if len(d)==0:
d=("SELECT Phoneno CUSTOMER4 where identification=?",user_input)
conn.commit()
else:
query1=("UPDATE CUSTOMER4 set Firstname=?,Lastname=?,Email=?,Phoneno=?) where identification=?")
values=(a,b,c,d,user_input)
cursor.execute(query1, (values,))
conn.commit()
print(cursor.rowcount,"record affected")
我像这样更改了我的代码..它正在执行但没有执行 updating.where 我是不是出错了?
查询1=("UPDATE CUSTOMER4 set Firstname=?,Lastname=?,Email=?,Phoneno=?) where identification=?")
括号())
您已经关闭了两次。
在 Phoneno=?)
之后删除右括号")"
所以在这里我想写一个python代码来更新一个table。就像我创建了一个名为 customers 的 table,其中包含一些列。我想更新从用户那里获取输入的列,如果用户没有提供任何输入,我希望它自己采用以前的值。请帮助我,我是 python.
的新手dummy=[]
user_input=int(input("Enter the identification number you want to change:"))
First_name=input("Enter the First name:")
if len(First_name)!=0:
dummy+="Firstname="+First_name
cursor.execute("update customer4 set Firstname=? where identification=?")
values=(dummy,user_input)
conn.commit
Last_name=input("Enter the last name:")
if len(Last_name)!=0:
if len(First_name)!=0:
dummy+="Firstname"+First_name, "Lastname"+Last_name
cursor.execute("update customer4 set Firstname=?, Lastname=? where identification=?")
values=(dummy,user_input)
conn.commit
else:
dummy+="Lastname"+Last_name
cursor.execute("update customer4 set Lastname=? where identification=?")
values=(dummy,user_input)
conn.commit
print(cursor.rowcount,"record affected")
这是我的 python 代码。在我学习的过程中帮助我 python.
您的 execute() 行的语法错误。使用 ”?”作为占位符是正确的方法,但您的值需要像这样插入:
cursor.execute("update customer4 set Firstname=? where identification=?", (dummy,user_input))
user_input=int(input("Enter the identification number you want to change:"))
query = "SELECT identification FROM CUSTOMER4 WHERE identification=?"
cursor.execute(query, (user_input,))
data = cursor.fetchall()
conn.commit()
if len(data) == 0:
print("Does not exist")
else:
print("Enter the first name:")
a=input()
print("Enter the last name:")
b=input()
print("Enter the email id:")
c=input()
print("Enter phone no:")
d=input()
if len(a)==0:
a=("SELECT Firstname from CUSTOMER4 where identification=?",user_input)
conn.commit()
if len(b)==0:
b=("SELECT Lastname from CUSTOMER4 where identification=?",user_input)
conn.commit()
if len(c)==0:
c=("SELECT Email from CUSTOMER4 where identification=?",user_input)
conn.commit()
if len(d)==0:
d=("SELECT Phoneno CUSTOMER4 where identification=?",user_input)
conn.commit()
else:
query1=("UPDATE CUSTOMER4 set Firstname=?,Lastname=?,Email=?,Phoneno=?) where identification=?")
values=(a,b,c,d,user_input)
cursor.execute(query1, (values,))
conn.commit()
print(cursor.rowcount,"record affected")
我像这样更改了我的代码..它正在执行但没有执行 updating.where 我是不是出错了?
查询1=("UPDATE CUSTOMER4 set Firstname=?,Lastname=?,Email=?,Phoneno=?) where identification=?")
括号())
您已经关闭了两次。 在 Phoneno=?)
之后删除右括号")"