0

我不确定为什么这不起作用。当我尝试运行循环的任何部分时,我得到 sqlite3.OperationalError: near "WHERE": 语法错误。

    con = sqlite3.connect('DatabaseName.sql')
    cur = con.cursor()

    if changes == "1":
        Fname = input("Enter new first name: ")
        Lname = input("Enter the last name of the person whom first name you wish to change: ")
        cur.execute("""UPDATE Contacts SET Fname WHERE Lname""")
        con.commit()
    elif changes == "2":
        Lname = input("Enter new last name: ")
        Fname = input("Enter the first name of the person whom last name you wish to change: ")
        cur.execute("UPDATE Contacts SET Lname WHERE Fname")
        con.commit()
    elif changes == "3":
        Phone = input("Enter new telephone number(no dashes or spaces): ")
        Fname = input("Enter the first name of the person whom telephone number you wish to change: ")
        Lname = input("Enter the last name of the person whom telephone number you wish to change: ")
        Phone = int(Phone)
        cur.execute("""UPDATE Contacts SET Phone WHERE Fname AND Lname""")
        con.commit()
4

1 回答 1

3

Python 和 SQL 是两种完全独立的语言;您不能直接从另一个访问变量。

要将 Python 变量的传递到 SQL 命令中,请使用参数,如下所示:

cur.execute("UPDATE Contacts SET FirstName = ? WHERE LastName = ?", (Fname, Lname))

(这里,FirstNameLastName是表中的列名,可能与 Python 变量名相同,也可能不同。)

于 2012-11-23T16:30:21.450 回答