![](/img/trans.png)
[英]Can't get text input from Tkinter Text widget into sqlite3 database
[英]Can't get my database in Sqlite3 to accept text
我有一个 python 程序正在尝试将条目写入数据库。 代码成功执行,并创建了销售表中的条目,但是当我在 DB Browser 中检查数据库中的 SQLite 时,它显示文本列为空白。 以下是代码的相关部分:
class SDB:
def __init__(self):
self.conn = sqlite3.connect("sales.db")
self.cur = self.conn.cursor()
self.cur.execute(
"CREATE TABLE IF NOT EXISTS sales (orderID INTEGER PRIMARY KEY, customer TEXT, itemPurchased TEXT)")
self.conn.commit()
def __del__(self):
self.conn.close()
def view(self):
self.cur.execute("SELECT * FROM sales")
rows = self.cur.fetchall()
return rows
def search(self, customer="", itemPurchased=""):
self.cur.execute("SELECT * FROM sales WHERE customer=? OR itemPurchased=?", (customer, itemPurchased,))
rows = self.cur.fetchall()
return rows
def purchase(self, customer, itemPurchased):
orderID=randint(1000, 9999)
self.cur.execute("INSERT INTO sales (orderID, customer, itemPurchased) VALUES (?, ?, ?)", (orderID, customer, itemPurchased,))
self.conn.commit()
print("Order Placed!")
sbd = SDB()
这是 python 中的数据库定义,function购买由 tkinter window 调用:
purchase_window = tk.Tk()
purchase_window.title('Purchase Books')
window_width = 300
window_height = 200
screen_width = purchase_window.winfo_screenwidth()
screen_height = purchase_window.winfo_screenheight()
center_x = int(screen_width / 2 - window_width / 2)
center_y = int(screen_height / 2 - window_height / 2)
purchase_window.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
itemLabel = ttk.Label(purchase_window, text="Book Title")
itemEntry = ttk.Entry(purchase_window)
customerLabel = ttk.Label(purchase_window, text="Your Name")
customerEntry = ttk.Entry(purchase_window)
itemPurchased = itemEntry.get()
customer = customerEntry.get()
orderButton = ttk.Button(purchase_window, text="Purchase", command=sbd.purchase(customer, itemPurchased))
itemLabel.grid(row=0, column=0)
itemEntry.grid(row=0, column=1)
customerLabel.grid(row=1, column=0)
customerEntry.grid(row=1, column=1)
orderButton.grid(row=2, column=1)
如您所见,代码应该插入客户姓名、他们订购的书名和随机生成的订单 ID。 订单 ID 很好地插入到数据库中,但文本项没有。 这是怎么回事?
您的代码中几乎没有问题:
orderID
列使用random.randint()
的结果可能会导致重复错误,请改用orderID
列上的AUTOINCREMENT
功能CREATE TABLE IF NOT EXISTS sales (orderID INTEGER PRIMARY KEY AUTOINCREMENT, customer TEXT, itemPurchased TEXT)
itemEntry
和customerEntry
后立即获得它们的内容,因此itemPurchased
和customer
由于尚未输入而为空字符串。 您需要在sbd.purchase()
中调用那些.get()
def purchase(self):
# get the required content from entries
customer = customerEntry.get()
itemPurchased = itemEntry.get()
# removed orderID as it is an autoincrement column now
self.cur.execute("INSERT INTO sales (customer, itemPurchased) VALUES (?, ?)", (customer, itemPurchased))
self.conn.commit()
print("Order Placed!")
command=sbd.purchase(customer, itemPurchased)
将立即执行sbd.purchase()
,而不是在单击按钮时执行。 使用command=sbd.purchase
并重新定义sbd.purchase()
而无需 arguments 如上所述。orderButton = ttk.Button(purchase_window, text="Purchase", command=sbd.purchase)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.