1
我有一個tkinter框架頁面,它有文本框輸入客戶詳細信息的名稱和地址。同一頁面還有一個保存按鈕,該按鈕鏈接到另一個將文本框中的詳細信息保存到.sqlite數據庫的方法。然而,當我點擊按鈕它說「sqlite3.InterfaceError:錯誤綁定參數1 - 可能不支持的類型。」 非常明顯的錯誤,但我不知道將輸入更改爲正確的輸入。請,任何幫助將是驚人的,謝謝!用python/tkinter/sqlite保存按鈕
代號爲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()
完美,這就是現在工作!謝謝。但是,應該清空文本字段框的saveDetails方法中的代碼行不起作用,因爲它說fNameBox未定義...:/ – user2302088 2013-04-20 14:57:52
@ user2302088這是一個完全不同的問題。 (而且Python是正確的;變量沒有在'SaveDetails'函數中定義。) – 2013-04-20 15:39:10