Mysql编写的语句没有被python中的值替换

时间:2017-07-31 23:42:49

标签: python python-3.x sqlite

尝试了(?)和(%s),但似乎没有效果。我哪里错了?

def update(phone,name):
    conn = sqlite3.connect(db)
    print ("\nOpened database for updates successfully")

    sql = "UPDATE VARUN set PHONE = %s where NAME= %s "
    print (sql)
    conn.execute(sql,(phone,name))
    '''
    conn.execute("UPDATE VARUN set PHONE = (?) where NAME= (?) ",(phone,name));
    '''
    conn.commit()

-----调用函数----

contactlist[selection()]=[nameVar.get(), phoneVar.get()]
updt = (contactlist[selection()])
name = (updt[0])
phone = (updt[1])
print (name,phone)

try:
    update(name,phone)
except:
    tkinter.messagebox.showwarning("cannot be blank")
else:

    setList ()
    saveContact()

1 个答案:

答案 0 :(得分:2)

首先,您正在使用bare except clause,这会阻止您查看函数引发的错误。删除它,看看它是如何失败的。

并且,您需要?个占位符而不包含左括号:

def update(phone, name):
    conn = sqlite3.connect(db)
    cursor = conn.cursor()
    print ("\nOpened database for updates successfully")

    sql = "UPDATE VARUN set PHONE = ? where NAME= ?"
    cursor.execute(sql, (phone, name))
    conn.commit()