简体   繁体   English

Python - SQLite - 查询无法正常工作的问题

[英]Python - SQLite - Issue with a query not working properly

This block of code takes user input and uses it to update information in a database.此代码块接受用户输入并使用它来更新数据库中的信息。

def Update_Save_Button(self, event = None):
    ticketinfo2 = self.ticketeu2.get()
    ticketinfo3 = self.ticketeu3.get()
    ticketinfo4 = self.ticketeu4.get()
    ticketinfo5 = self.ticketeu5.get()
    ticketinfo6 = self.ticketeu6.get()
    if  len(ticketinfo2) == 0 or \
        len(ticketinfo3) == 0 or \
        len(ticketinfo4) == 0 or \
        len(ticketinfo5) == 0 or \
        len(ticketinfo6) == 0:
        messagebox.showerror("Error", "Please check that all fields have been entered.")
    else:
        self.cursor.execute ('''
                            UPDATE 
                                "Service Tickets"  
                            SET
                                "Reason for Visit" = ?,
                                "WarrantyVPO" = ?,
                                "Date Ticket Received" = ?,
                                "Date of Service" = ?,
                                "Service Person" = ?
                            WHERE
                                "ServiceTicketsId" = ?;
                            ''',
                            (ticketinfo2, ticketinfo3, ticketinfo4, ticketinfo5, ticketinfo6, self.text1,))
        self.con.commit()
        self.Tree_Refresh   (self.SecondTree, 
                                ('''
                                SELECT 
                                    * 
                                FROM
                                    "Service Tickets"
                                WHERE 
                                    JobID = ?
                                '''),
                                (self.JobID,))
        self.LoadUpdate.destroy()

I have many other blocks of code where queries are being run, and they all run correctly.我还有许多其他正在运行查询的代码块,它们都运行正确。 This block of code, however, is not operating correctly.但是,此代码块运行不正确。 Not only that, but there are no errors being yielded in the terminal.不仅如此,终端中也没有产生错误。 I have had issues with improper syntax in the past with previous queries, and I tried to use what I learned to fix the current problem.过去我在以前的查询中遇到过语法不正确的问题,我尝试使用我学到的知识来解决当前的问题。 I have also tried adding, moving, and removing lines of code.我还尝试过添加、移动和删除代码行。 I would include specifics, but unfortunately I've done so much that I lost track of what all I've tried.我会包括细节,但不幸的是,我做了很多事情,以至于我忘记了我所做的一切。

I will say, however, that when I removed the WHERE section of my query, the UPDATE worked.但是,我会说,当我删除查询的 WHERE 部分时,UPDATE 起作用了。 It updated all of the data in the table, albeit some of the data appeared to be incorrect.它更新了表中的所有数据,尽管有些数据似乎不正确。

I would appreciate any advice.我会很感激任何建议。

since removing "WHERE" clause from UPDATE statement makes it to update all the rows, I'd assume the condition "ServiceTicketsId =?"由于从 UPDATE 语句中删除“WHERE”子句使其更新所有行,我假设条件“ServiceTicketsId =?” is always false.总是假的。 Check the parameter value and make sure "self.text1" (I believe this is the value you are passing over to WHERE-clause) contains an existing ticket id.检查参数值并确保“self.text1”(我相信这是您传递给 WHERE 子句的值)包含现有的票证 ID。

Annoyingly enough, the solution was to convert self.text1 to a string烦人的是,解决方案是将 self.text1 转换为字符串

else:
        self.text1 = str(self.text1)
        self.cursor.execute ('''
                            UPDATE 
                                "Service Tickets"  
                            SET
                                "Reason for Visit" = ?,
                                "WarrantyVPO" = ?,
                                "Date Ticket Received" = ?,
                                "Date of Service" = ?,
                                "Service Person" = ?
                            WHERE
                                "ServiceTicketsId" = ?;
                            ''',
                            (ticketinfo2, ticketinfo3, ticketinfo4, ticketinfo5, ticketinfo6, self.text1,))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM