导致TypeError

时间:2019-04-04 16:50:31

标签: python sqlite tkinter

我正在使用Tkinter和Sqlite开发小型数据库。对于我的功能之一,我将其设置为从列表框中更新任何选定的元组。我不断收到类型错误,希望有人可以帮助我。

我已经查看了我的前端代码(GUI)和后端代码(SQLITE),但仍然无法弄清它在说什么。我可能正在看一些简单的东西。

后端(代码中包含错误)

def update(Model, Serial, type, Test1, Test2, Test3, Test4, Duration, Failure, Comments):

    '''cur.execute("UPDATE Ml SET Model="", Serial="", Type="", Test1="", Test2="", Test3="", Test4="", Duration="", Failure="", Comments="",  where id=?" (Model, Serial, Type, Test1, Test2, Test3, Test4, Duration, Failure, Comments, id))
TypeError: 'str' object is not callable'''

    conn = sqlite3.connect("Antenna.db")
    cur = conn.cursor()
    cur.execute("UPDATE Ml SET Model="", Serial="", Type="", Test1="", Test2="", Test3="", Test4="", Duration="", Failure="", Comments="",  where id=?" (Model, Serial, Type, Test1, Test2, Test3, Test4, Duration, Failure, Comments, id))
    conn.commit()
    conn.close()

前端


def update_command():

    result = messagebox.askquestion("Edit Record?", "Are you sure you'd like to change the following record?")
    if result == "yes" and model_text.get() and serial_text.get() and Type_text.get() and Test1_text.get() and Test2_text.get() and Test3_text.get() and Test4_text.get() and duration_text.get() and failure_text.get() and comment_text.get():
        Back_End.update(model_text.get(), serial_text.get(), Type_text.get(), Test1_text.get(), Test2_text.get(), Test3_text.get(), Test4_text.get(), duration_text.get(), failure_text.get(), comment_text.get())
        view_command()
    else:
        messagebox.showerror("Invalid Selection", "Please Select a item to edit")

3 个答案:

答案 0 :(得分:2)

您对游标execute的调用语法似乎充满了问题。尝试使用此版本:

cur.execute("""UPDATE Ml SET Model=?, Serial=?, Type=?, Test1=?, Test2=?, Test3=?,
   Test4=?, Duration=?, Failure=?, Comments=?, WHERE id=?""",
    (Model, Serial, Type, Test1, Test2, Test3, Test4, Duration, Failure, Comments, id,))

我看到的问题包括您应该在所有列的更新分配的RHS中使用?占位符。当前,您正在使用空字符串,但是无论如何都会导致另一个错误,因为您使用双引号而不是单引号。

答案 1 :(得分:0)

    '''cur.execute("UPDATE Ml SET Model="", Serial="", Type="", Test1="", Test2="", Test3="", Test4="", Duration="", Failure="", Comments="",  where id=?", (Model, Serial, Type, Test1, Test2, Test3, Test4, Duration, Failure, Comments, id))

您错过了一个逗号。在字符串结束和参数元组开始之后放置一个。

答案 2 :(得分:0)

我同意@Bryan Oakley的观点,对我而言,最大的问题是sql语句的构建方式。

请参见下文,这应该简化所有事情。

我已经将语句的各个部分解耦了。

table = 'Ml'

fields = ('Model', 'Serial', 'Type', 'Test1', 'Test2', 'Test3', 'Test4', 'Duration', 'Failure', 'Comments',)

pk = 'id'

sql = "UPDATE %s SET %s =? WHERE %s =?"%(table," =?, ".join(fields),pk)

args = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9,)

args = (*args, 42)

print(sql,args)