当我更新的属性是用户输入时,如何编写更新语句?

时间:2017-12-04 20:55:09

标签: python sql

def updateNurseSpecification():

    nurse_lname = input("\nEnter the last name of the nurse "
                    "you would to update: \n")

    query = c.execute('SELECT fname, lname, specification FROM Nurses '
                  'WHERE lname LIKE ?', (nurse_lname,))

    new_spec = input("\nEnter the updated specification of this employee: \n")

    query1 = c.execute('UPDATE Nurses SET specification = ', (new_spec,)
                  ' WHERE lname LIKE ?', (nurse_lname,))

我不知道使用变量“new_spec”更新记录的语法。

    print("Your update is as shown: \t")

    query2 = c.execute('SELECT fname, lname, specification FROM Nurses'
                 'WHERE lname LIKE ?', (nurse_lname,))    

1 个答案:

答案 0 :(得分:2)

execute有两个参数 - 一个SQL字符串和一个参数元组:

c.execute('UPDATE Nurses SET specification = %s WHERE lname LIKE %s', 
          (new_spec, nurse_lname))