Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
I';我试图匹配python中的用户输入,以匹配数据库python-SQL中的值_Python_Sql_Python 3.x - Fatal编程技术网

I';我试图匹配python中的用户输入,以匹配数据库python-SQL中的值

I';我试图匹配python中的用户输入,以匹配数据库python-SQL中的值,python,sql,python-3.x,Python,Sql,Python 3.x,最后,我试图找出一种方法,从Python更新数据库中的单个值 我的数据库由一个employee表组成。此表中的值为: Eno (Employee ID) fname (first name) Salary Sex 这个python程序的3个选项是: 添加到数据库(完成) 查看数据库(完成) 更新数据库(帮助) 我已经想出了一种方法来做到这一点,但不幸的是,它改变了所有相同的数据。例如,如果我改变性别男-女,所有男性的值都会改变 到目前为止,这是选项1和2的代码 while True:

最后,我试图找出一种方法,从Python更新数据库中的单个值

我的数据库由一个employee表组成。此表中的值为:

Eno (Employee ID)
fname (first name)
Salary
Sex
这个python程序的3个选项是:

  • 添加到数据库(完成)
  • 查看数据库(完成)
  • 更新数据库(帮助)
我已经想出了一种方法来做到这一点,但不幸的是,它改变了所有相同的数据。例如,如果我改变性别男-女,所有男性的值都会改变

到目前为止,这是选项1和2的代码

while True:                                                                    
                                                                              
   print("====== Welcome to the Employee Database ======")                    
   print("===== Please select from the menu ======")                          
   print(" 1. Add An Employee.")                                              
   print(" 2. Update The Database.")                                          
   print(" 3. View The Database.")                                            
   print(" 5. Remove an employee.")                                           
   print(" 4. Exit")                                                          
   action = input()                                                           
                                                                              
   if action == '1':                                                          
       vfname = input("Enter employee name?")                                 
       vsalary = input("Enter employee salary")                               
       vsex = input("Enter employee gender")                                  
       sql = "INSERT INTO Employees (fname, salary, sex) VALUES (%s, %s, %s)" 
       vals = (vfname, vsalary, vsex)                                         
       mycursor.execute(sql, vals)                                            
       mydb.commit()                                                          
       mycursor.execute("SELECT * FROM Employees")                            
       myresult = mycursor.fetchall()                                         
       for x in myresult:                                                     
           print(x)                                                           
                                                                              
   elif action == '2':                                                        
       mycursor.execute("SELECT Eno, fname FROM Employees")                   
       record = mycursor.fetchall()                                           
       for x in record:                                                       
           print(x)  
我使用此代码来更新表,但它并不健壮:

beforeName = input("Who's name do you wish to change? ")              
afterName = input("Please enter the new name of " + beforeName +" ")  
changename = (afterName, beforeName)                                  
sql = "UPDATE Employees SET fname = %s WHERE fname = %s"              
print("--- Name changed from " +beforeName+ " to " +afterName+ " ---")
mycursor.execute(sql, changename)                                     
mydb.commit() 
这是可行的,但例如,如果我有一名员工的名字是Ryan,我想和John一起更改Ryans的名字,那么所有其他Ryans也会更改。我无法更改属性,因为它们是讲师为我设置的

实际上,我希望请求员工id(用户输入)并将其与数据库中的员工id相匹配,因此我可以根据该员工id更改或更新数据

我希望我的代码如下所示:

System: Prints IDS & Fnames
SYSTEM: Ask user which employee they wish to change (enter employee id)
SYSTEM: once the user Id is inputted, a menu will come up to change (change name, salary,sex)
SYSTEM: if statement for user input
USER: enters which data
SYSTEM: asks for new data to be inputted
谢谢你的帮助,希望你能理解我想说的