首页 文章

使用python / tkinter / sqlite保存按钮

提问于
浏览
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()

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())]
    

相关问题