在搜索另一个SQL选项卡时更新SQL表

2024-04-20 14:20:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个SQL数据库“车库.db“有3个表: 客户、汽车和汽车

当有人在Car表中输入注册信息时,我想更新MOT表中的BookedMOT字段。有人能帮我用SQL查询,可以做到这一点,谢谢。你知道吗

我用tkinter在Python3.6中编写了这个代码。这是我的尝试

    def Booked(self):
        date = Date.get()
        month = Month.get()
        year = Year.get()
        BookedMOT = (date + '/' + month + '/' + year)
        Registration = self.RegistrationEnt.get()
        with sqlite3.connect('Garage.db') as db:
            cursor = db.cursor()

        add_date = ('UPDATE MOT SET MOT.BookedMOT = ? FROM Car WHERE Car.Registration = ?')
        cursor.execute(add_date,[(BookedMOT), (Registration)])
        db.commit()

Tags: selfadd数据库dbsqlgetdateregistration
1条回答
网友
1楼 · 发布于 2024-04-20 14:20:14

(这解决了一些Python问题,我甚至在意识到SQL看起来不对劲之前就注意到了,这可能应该首先修复)

试试这个:

with sqlite3.connect('Garage.db') as db:
    db.execute(add_date, (BookedMOT, Registration))

一般来说,当您说with ... as x:时,您可能应该在with块内使用x。块完成后,它会自动提交,然后尝试使用dbcursor可能是不正确的。with也意味着你不必再db.commit()。你知道吗

sqlite3 connection objects (^{} in this case) have an ^{} method:

This is a nonstandard shortcut that creates a cursor object by calling the cursor() method, calls the cursor’s execute() method with the parameters given, and returns the cursor.

最后,有一些多余的括号可以删除。你知道吗

相关问题 更多 >