gpt4 book ai didi

python - 使用 python/tkinter/sqlite 保存按钮

转载 作者:太空宇宙 更新时间:2023-11-03 19:00:47 25 4
gpt4 key购买 nike

我有一个 tkinter 框架页面,其中包含用于输入客户详细信息的姓名和地址的文本框。同一页面还有一个保存按钮,该按钮链接到另一个方法,该方法将文本框中的详细信息保存到 .sqlite 数据库。但是,当我单击按钮时,它显示“sqlite3.InterfaceError:绑定(bind)参数 1 错误 - 可能不受支持的类型。”这是一个不言自明的错误,但我不知道将输入更改为正确的输入。拜托,任何帮助都会很棒,谢谢!

tk 帧间和页面布局 + 保存按钮的代码:

import tkinter as tk
import sqlite3

class Page(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
def show(self):
self.lift()

class Page1(Page):

fName = ""
sName = ""
address1 = ""
address2 = ""
city = ""
postCode = ""

def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)

#Set up labels
spaceLabel = tk.Label(self, text="")
fNameLabel = tk.Label(self, text="First Name: ")
sNameLabel = tk.Label(self, text="Last Name: ")
address1Label = tk.Label(self, text="Address Line 1: ")
cityLabel = tk.Label(self, text="City: ")
postCodeLabel = tk.Label(self, text="Post Code: ")

#Create string variables
self.fName = tk.StringVar()
self.sName = tk.StringVar()
self.address1 = tk.StringVar()
self.address2 = tk.StringVar()
self.city = tk.StringVar()
self.postCode = tk.StringVar()

#Set up text entry boxes
fNameBox = tk.Entry(self, textvariable = self.fName)
sNameBox = tk.Entry(self, textvariable = self.sName)
address1Box = tk.Entry(self, textvariable = self.address1)
address2Box = tk.Entry(self, textvariable = self.address2)
cityBox = tk.Entry(self, textvariable = self.city)
postCodeBox = tk.Entry(self, textvariable = self.postCode)

#Arrange Labels
spaceLabel.grid(row=0, column=0)
fNameLabel.grid(row=1, column=0, padx = 10, pady = 10)
sNameLabel.grid(row=1, column=2, padx = 10, pady = 10)
address1Label.grid(row=2, column=0, padx = 10, pady = 10)
address2Label.grid(row=3, column=0, padx = 10, pady = 10)
cityLabel.grid(row=4, column=0, padx = 10, pady = 10)
postCodeLabel.grid(row=5, column=0, padx = 10, pady = 10)

#Arrange text entry boxes
fNameBox.grid(row=1, column=1, padx = 10, pady = 10)
sNameBox.grid(row=1, column=3, padx = 10, pady = 10)
address1Box.grid(row=2, column=1, padx = 10, pady = 10)
address2Box.grid(row=3, column=1, padx = 10, pady = 10)
cityBox.grid(row=4, column=1, padx = 10, pady = 10)
postCodeBox.grid(row=5, column=1, padx = 10, pady = 10)

#Save Details button
saveCustomerDetails = tk.Button(self, text = "Save Details", command = self.SaveDetails)
saveCustomerDetails.pack()
saveCustomerDetails.grid(row=6,column=2,padx = 20, pady = 20)

#When you click one save button, it should use this method to add the data
# entry field text to the database.

def SaveDetails(self):
conn = sqlite3.connect('lanyard.db')
c = conn.cursor()
c.execute('PRAGMA foreign_keys = ON')
conn.commit()

customerData = [(None, self.fName, self.sName, self.address1, self.address2, self.city, self.postCode)]

for element in customerData:
c.execute("INSERT INTO Customers VALUES (?,?,?,?,?,?,?)", element)
conn.commit()

c.close()
conn.close()

fNameBox.delete(0, END)
sNameBox.delete(0, END)
address1Box.delete(0, END)
address2Box.delete(0, END)
cityBox.delete(0, END)
postCodeBox.delete(0, END)

print ("Saved")

SQLite数据库创建如下:

import sqlite3
conn = sqlite3.connect('lanyard.sqlite')
cursor = conn.cursor()

#Customers
cursor.execute('''DROP TABLE IF EXISTS Archive''')
cursor.execute('''CREATE TABLE IF NOT EXISTS Customers(
Customer_Id INTEGER PRIMARY KEY,
First_Name TEXT(20),
Last_Name TEXT(20),
Address_1 TEXT(20),
Address_2 TEXT(20),
City TEXT(20),
Post_Code TEXT(20))''')

cursor.execute('''PRAGMA foreign_keys = ON''')

cursor.close()
conn.commit()
conn.close()

最佳答案

sqlite 不知道 StringVar 是什么——您必须向其传递正常值。尝试更改您的 customerData 列表,如下所示:

customerData = [(None, self.fName.get(), self.sName.get(), 
self.address1.get(), self.address2.get(),
self.city.get(), self.postCode.get())]

关于python - 使用 python/tkinter/sqlite 保存按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16120474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com