在 SQLite3 DELETE 语句中引用 python 变量 [英] Referring to a python variable in SQLite3 DELETE statement

查看:30
本文介绍了在 SQLite3 DELETE 语句中引用 python 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个函数来从表中删除一条记录.我试图在删除语句中引用的变量是 Tkinter 输入字段.

I am writing a function to delete a record from a table. The variable I am trying to refer to in the delete statement is a Tkinter entry field.

我使用 .get() 方法获取变量,但我无法在不返回错误的情况下将其传递到 SQLite 语句中.

I get the variable using the .get() method, but I can't then pass this into the SQLite statment without returning an error.

以下代码是框架的一部分,我只是在问题中添加了相关代码

The following code is part of the frame, I've only added the relevant code to the problem

from tkinter import *

import sqlite3

class EditOrdersForm(Frame):

        def __init__(self):

        Frame.__init__(self)
        self.master.title("Edit Orders form:")
        self.pack()

        def displayDelOrderOptions(self):
            self.deleteOrderOptionsLabel = Label(self, text="Enter the Order ID of the order you wish to delete: ").grid(row=numOrders+4, pady=2, sticky=W)
            self.deleteOrderOptionsEntry = Entry(self, bd=3, width=10)
            self.deleteOrderOptionsEntry.grid(row=numOrders+4, pady=5, column=1)
            global orderToDelete
            orderToDelete = self.deleteOrderOptionsEntry.get()
            self.deleteButton = Button(self, text = "Delete this order", command = self.deleteOrder)
            self.deleteButton.grid(row=numOrders+5, pady=5, column=1)
        def deleteOrder(self):
            conn = sqlite3.connect("testdb.db")
            c = conn.cursor()

            c.execute("DELETE FROM orders WHERE orders_id=:1)", (orderToDelete,))

            conn.commit
            c.close()

root = EditOrdersForm()
root.mainloop()

我遇到的问题是 WHERE 语句,我如何引用 orderToDelete.我尝试将其转换为字符串并使用 (?) 作为参数,但这也不起作用.

The problem I have is with the WHERE statement, how do I refer to orderToDelete. I have tried converting it to a string and using (?) as the parameter but this didn't work either.

推荐答案

参数化的正确语法查询是:

c.execute("DELETE FROM orders WHERE orders_id=?)", (orderToDelete,))

我相信你只需要调用 commit 而不是引用它

I believe you just need to call commit instead of reference it

conn.commit()

这篇关于在 SQLite3 DELETE 语句中引用 python 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆