1

我有一个 tkinter 框架页面,其中包含用于输入客户详细信息的名称和地址的文本框。同一页面还有一个保存按钮,该按钮链接到另一种方法,该方法将文本框中的详细信息保存到 .sqlite 数据库。但是,当我单击按钮时,它显示“sqlite3.InterfaceError:错误绑定参数 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()
4

1 回答 1

1

sqlite 不知道是什么StringVar——你必须传递正常值。尝试像这样更改您的customerData列表:

customerData = [(None, self.fName.get(), self.sName.get(), 
                 self.address1.get(), self.address2.get(), 
                 self.city.get(), self.postCode.get())]
于 2013-04-20T14:29:11.867 回答