使用 python/tkinter/sqlite 保存按钮 [英] Save button with python/tkinter/sqlite

查看:40
本文介绍了使用 python/tkinter/sqlite 保存按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a tkinter frame page which has text boxes to type in name and address for customer details. The same page also has a save button that is linked to another method that saves the details in the text boxes to an .sqlite database. However when I click the button it says "sqlite3.InterfaceError: Error binding parameter 1 - probably unsupported type." Quite a self explanatory error but I have no idea to change the input to the right one. Please, any help will be amazing, thanks!

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

Code for the tk inter frame and page layout + save button:

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 数据库创建如下:

SQLite database is created as follows:

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 列表:

sqlite has no idea what StringVar is -- you must pass it normal values. Try changing your customerData list like so:

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

这篇关于使用 python/tkinter/sqlite 保存按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆