使用 python tkinter 在 mysql 中存储日期时出现问题

发布于 2025-01-12 16:08:21 字数 8905 浏览 3 评论 0原文

可能的问题是什么,因为每次我选择日期时都会出现错误“列计数与值计数和第 1 行不匹配”。这些列是正确的,我还将日期条目转换为字符串。但它就出来了。我想将它存储在 MySQL 数据库中。希望有人能帮忙提前谢谢。

这是我的完整代码:

from tkinter import *
from tkinter.ttk import Combobox 
from tkcalendar import DateEntry
from tkinter import ttk
from tkinter import messagebox
import mysql.connector



def addData():
    
    try:
        
        full_name = full_name_entry.get()
        
        if full_name == "":
            messagebox.showerror(title = "Save Record Error", message = "You must fill field first")
        else:
            connection = mysql.connector.connect(
                host = 'localhost',
                username = 'root',
                password = '',
                database = 'scratch_database'
            )
            
            my_cursor = connection.cursor()
            
            my_cursor.execute("insert into scratch_table values(%s,%s,%s,%s,%s)", 
                              (fullName.get(),
                               srProject.get(),
                               contractStart.get(),
                               contractEnd.get(),
                               stat.get()
                               
                               ))
            
            connection.commit()
            
            connection.close()
            
            messagebox.showinfo(title = "Information", message = "Record Saved!")
    
    except Exception as e:
        
        messagebox.showerror(title = "Scratch Data", message = e)

def databaseConnection():
    
    try:
        
        connection = mysql.connector.connect(
            host = 'localhost',
            username = 'root',
            password = '',
            database = 'scratch_database'
        )
        
        my_cursor = connection.cursor()
        
        messagebox.showinfo(title = "Information", message = "You are connected to database")
        
        connection.commit()
        
        connection.close()
        
    
    except Exception as e:
        
        messagebox.showerror(title = "Connection Error", message = e)



def mainWindow():
    

    window = Tk()
        
    window.config(bg = 'white')
    window.title("Sample Table for SR Data")
        
    window.resizable(0,0)
    window.geometry('1212x771')
    
    global fullName
    global srProject
    global contractStart
    global contractEnd
    global stat

    fullName = StringVar()
    srProject = StringVar()
    contractStart = StringVar()
    contractEnd = StringVar()
    stat = StringVar()
        
        
    # FULLNAME LABEL
    full_name = Label(window, text = "Full Name", font = ('Arial', 12), bg = 'white')
    full_name.place(x = 30, y = 63)
        
    global full_name_entry
    
    # FULLNAME ENTRY
    full_name_entry = Entry(window, font = ('Arial', 12), width = 30, highlightbackground = 'gray', highlightcolor = 'black',
                                highlightthickness = 1, bd = 0, textvariable = fullName)
    full_name_entry.place(x = 34, y = 101)
        
        
    # PROJECTS LABEL
    projects = Label(window, text = "Positions", font = ('Arial', 12), bg = 'white')
    projects.place(x = 30, y = 156)
        
        
    # POSITIONS OPTIONS
    projects_options = ['Agricultural Situation Report', 'ASPBI', 'BLPS', 'BPCS', 'CPH', 'Census Evaluation Survey',
                        'Cities and Municipalities Competitive Index','CLPS', 'CSS', 'CBMS', 'Consumer Expectation Survey',
                        'Other Crop Survey', 'PCPS', 'PCSS', 'PPS', 'QaFS/QiFS', 'QSPBI', 'WSP/RPS', 'SICT', 'SOF', 'UGBSEA',
                        'ULE', 'CPS', 'CDSPDP', 'CLPS', 'SCMDV for 2021 PPCM', 'CDC', 'Quarterly Crops Production Survey', 
                        '2021 CLPS','MPCSRS/PCSS/CSS 2021', 'January 2022 FPS'
                        ]
        
        
    # POSITIONS COMBOBOX
    projects_combo_box = Combobox(window, values = projects_options, width = 28, font = ('Arial', 12), cursor = 'hand2', textvariable = srProject)
    projects_combo_box.place(x = 34, y = 192)
        
        
    # PREVENTS A TYPING VALUE IN COMBO BOX
    projects_combo_box['state'] = 'readonly'
        
        
    # START OF CONTRACT LABEL
    start_of_contract = Label(window, text = "Start of Contract", font = ('Arial', 12), bg = 'white')
    start_of_contract.place(x = 30, y = 261)
        
        
    # START OF CONTRACT DATE TIME PICKER
    start_of_contract_date = DateEntry(window, selectmode = 'day', year = 2022, month = 1, day = 1, cursor = 'hand2',
                                        font = ('Arial', 12), width = 28, textvariable = contractStart)
    start_of_contract_date.place(x = 34, y = 299)
        
        
    # END OF CONTRACT LABEL
    end_of_contract = Label(window, text = "End of Contract", font = ('Arial', 12), bg = 'white')
    end_of_contract.place(x = 30, y = 362)
        
        
    # END OF CONTRACT DATE TIME PICKER
    end_of_contract_date = DateEntry(window, selectmode = 'day', year = 2022, month = 1, day = 1, cursor = 'hand2',
                                        font = ('Arial', 12), width = 28, textvariable = contractEnd)
    end_of_contract_date.place(x = 34, y = 400)   
        
        
    # STATUS LABEL
    status = Label(window, text = "Positions", font = ('Arial', 12), bg = 'white')
    status.place(x = 30, y = 464)
        
        
    # STATUS OPTIONS
    status_options = ['Active', 'Inactive', 'Terminated', 'Blacklisted']
        
        
    # STATUS COMBOBOX
    status_combo_box = Combobox(window, values = status_options, width = 28, font = ('Arial', 12), cursor = 'hand2', textvariable = stat)
    status_combo_box.place(x = 34, y = 500)
        
        
    # PREVENTS A TYPING VALUE IN COMBO BOX
    status_combo_box['state'] = 'readonly'
        
        
    # SAVE BUTTON
    save = Button(window, text = "Save", font = ('Arial', 12), padx = 43,  pady = 5, cursor = 'hand2', bg = 'green', fg = 'white', bd = 0,
                    activebackground = 'green', activeforeground = 'white', command = addData)
    save.place(x = 34, y = 573)
        
        
    # EDIT BUTTON
    edit = Button(window, text = "Edit", font = ('Arial', 12), padx = 45,  pady = 5, cursor = 'hand2', bg = 'orange', fg = 'white',
                    activebackground = 'orange', activeforeground = 'white', bd = 0)
    edit.place(x = 180, y = 573)
        
    # DELETE BUTTON
    delete = Button(window, text = "Delete", font = ('Arial', 12), padx = 45,  pady = 5, cursor = 'hand2', bg = 'red', fg = 'white',
                    activebackground = 'red', activeforeground = 'white', bd = 0)
    delete.place(x = 93, y = 648)
        
        
    # CONNECTION BUTTON
    connection_button = Button(window, text = "Check Stat", font = ('Arial', 12), padx = 10,  pady = 5, cursor = 'hand2', bg = 'blue', fg = 'white',
                    activebackground = 'blue', activeforeground = 'white', bd = 0, command = databaseConnection)
    connection_button.place(x = 1064, y = 648)
        
        
    # SEARCH LABEL
    search = Label(window, text = "Search", font = ('Arial', 12), bg = 'white')
    search.place(x = 878, y = 44)
        
        
    # SEARCH ENTRY
    search_entry = Entry(window, font = ('Arial', 12), width = 15, highlightbackground = 'gray', highlightcolor = 'black',
                                highlightthickness = 1, bd = 0)
    search_entry.place(x = 966, y = 41)



    # Create Treeview Frame
    tree_frame = Frame(window)
    tree_frame.pack(pady=20)

    # Treeview Scrollbar
    tree_scroll = Scrollbar(tree_frame)
    tree_scroll.pack(side=RIGHT, fill=Y)

    # Create Treeview
    my_tree = ttk.Treeview(tree_frame, yscrollcommand=tree_scroll.set, selectmode="extended")

    # Pack to the screen
    my_tree.pack(side= LEFT)

    #Configure the scrollbar
    tree_scroll.config(command=my_tree.yview)

    # Define Our Columns
    my_tree['columns'] = ("SRID", "fullname", "projects", "startofcontract", "endofcontract", "status")

    # Formate Our Columns
    my_tree.column("#0", width=0, stretch=NO)
    my_tree.column("SRID", anchor=W, width=140)
    my_tree.column("fullname", anchor=CENTER, width=100)
    my_tree.column("projects", anchor=W, width=140)
    my_tree.column("startofcontract", anchor=CENTER, width=140)
    my_tree.column("endofcontract", anchor=W, width=140)
    my_tree.column("status", anchor=CENTER, width=140)

    # Create Headings 
    my_tree.heading("#0", text="", anchor=W)
    my_tree.heading("SRID", text="SRID", anchor=W)
    my_tree.heading("fullname", text="FullName", anchor=CENTER)
    my_tree.heading("projects", text="Projects", anchor=W)
    my_tree.heading("startofcontract", text = "Start", anchor=W)
    my_tree.heading("endofcontract", text = "End", anchor=W)
    my_tree.heading("status", text = "Status", anchor=W)


        
    window.mainloop()

mainWindow()

What is the possible problem because every time I select a date there is an error which is "Column count doesn't match value count and row 1". The columns are correct and I also converted the date entry to string. But there it comes out. I would like to store it in the MySQL database. Hope someone can help thank you in advance.

Here's my full code:

from tkinter import *
from tkinter.ttk import Combobox 
from tkcalendar import DateEntry
from tkinter import ttk
from tkinter import messagebox
import mysql.connector



def addData():
    
    try:
        
        full_name = full_name_entry.get()
        
        if full_name == "":
            messagebox.showerror(title = "Save Record Error", message = "You must fill field first")
        else:
            connection = mysql.connector.connect(
                host = 'localhost',
                username = 'root',
                password = '',
                database = 'scratch_database'
            )
            
            my_cursor = connection.cursor()
            
            my_cursor.execute("insert into scratch_table values(%s,%s,%s,%s,%s)", 
                              (fullName.get(),
                               srProject.get(),
                               contractStart.get(),
                               contractEnd.get(),
                               stat.get()
                               
                               ))
            
            connection.commit()
            
            connection.close()
            
            messagebox.showinfo(title = "Information", message = "Record Saved!")
    
    except Exception as e:
        
        messagebox.showerror(title = "Scratch Data", message = e)

def databaseConnection():
    
    try:
        
        connection = mysql.connector.connect(
            host = 'localhost',
            username = 'root',
            password = '',
            database = 'scratch_database'
        )
        
        my_cursor = connection.cursor()
        
        messagebox.showinfo(title = "Information", message = "You are connected to database")
        
        connection.commit()
        
        connection.close()
        
    
    except Exception as e:
        
        messagebox.showerror(title = "Connection Error", message = e)



def mainWindow():
    

    window = Tk()
        
    window.config(bg = 'white')
    window.title("Sample Table for SR Data")
        
    window.resizable(0,0)
    window.geometry('1212x771')
    
    global fullName
    global srProject
    global contractStart
    global contractEnd
    global stat

    fullName = StringVar()
    srProject = StringVar()
    contractStart = StringVar()
    contractEnd = StringVar()
    stat = StringVar()
        
        
    # FULLNAME LABEL
    full_name = Label(window, text = "Full Name", font = ('Arial', 12), bg = 'white')
    full_name.place(x = 30, y = 63)
        
    global full_name_entry
    
    # FULLNAME ENTRY
    full_name_entry = Entry(window, font = ('Arial', 12), width = 30, highlightbackground = 'gray', highlightcolor = 'black',
                                highlightthickness = 1, bd = 0, textvariable = fullName)
    full_name_entry.place(x = 34, y = 101)
        
        
    # PROJECTS LABEL
    projects = Label(window, text = "Positions", font = ('Arial', 12), bg = 'white')
    projects.place(x = 30, y = 156)
        
        
    # POSITIONS OPTIONS
    projects_options = ['Agricultural Situation Report', 'ASPBI', 'BLPS', 'BPCS', 'CPH', 'Census Evaluation Survey',
                        'Cities and Municipalities Competitive Index','CLPS', 'CSS', 'CBMS', 'Consumer Expectation Survey',
                        'Other Crop Survey', 'PCPS', 'PCSS', 'PPS', 'QaFS/QiFS', 'QSPBI', 'WSP/RPS', 'SICT', 'SOF', 'UGBSEA',
                        'ULE', 'CPS', 'CDSPDP', 'CLPS', 'SCMDV for 2021 PPCM', 'CDC', 'Quarterly Crops Production Survey', 
                        '2021 CLPS','MPCSRS/PCSS/CSS 2021', 'January 2022 FPS'
                        ]
        
        
    # POSITIONS COMBOBOX
    projects_combo_box = Combobox(window, values = projects_options, width = 28, font = ('Arial', 12), cursor = 'hand2', textvariable = srProject)
    projects_combo_box.place(x = 34, y = 192)
        
        
    # PREVENTS A TYPING VALUE IN COMBO BOX
    projects_combo_box['state'] = 'readonly'
        
        
    # START OF CONTRACT LABEL
    start_of_contract = Label(window, text = "Start of Contract", font = ('Arial', 12), bg = 'white')
    start_of_contract.place(x = 30, y = 261)
        
        
    # START OF CONTRACT DATE TIME PICKER
    start_of_contract_date = DateEntry(window, selectmode = 'day', year = 2022, month = 1, day = 1, cursor = 'hand2',
                                        font = ('Arial', 12), width = 28, textvariable = contractStart)
    start_of_contract_date.place(x = 34, y = 299)
        
        
    # END OF CONTRACT LABEL
    end_of_contract = Label(window, text = "End of Contract", font = ('Arial', 12), bg = 'white')
    end_of_contract.place(x = 30, y = 362)
        
        
    # END OF CONTRACT DATE TIME PICKER
    end_of_contract_date = DateEntry(window, selectmode = 'day', year = 2022, month = 1, day = 1, cursor = 'hand2',
                                        font = ('Arial', 12), width = 28, textvariable = contractEnd)
    end_of_contract_date.place(x = 34, y = 400)   
        
        
    # STATUS LABEL
    status = Label(window, text = "Positions", font = ('Arial', 12), bg = 'white')
    status.place(x = 30, y = 464)
        
        
    # STATUS OPTIONS
    status_options = ['Active', 'Inactive', 'Terminated', 'Blacklisted']
        
        
    # STATUS COMBOBOX
    status_combo_box = Combobox(window, values = status_options, width = 28, font = ('Arial', 12), cursor = 'hand2', textvariable = stat)
    status_combo_box.place(x = 34, y = 500)
        
        
    # PREVENTS A TYPING VALUE IN COMBO BOX
    status_combo_box['state'] = 'readonly'
        
        
    # SAVE BUTTON
    save = Button(window, text = "Save", font = ('Arial', 12), padx = 43,  pady = 5, cursor = 'hand2', bg = 'green', fg = 'white', bd = 0,
                    activebackground = 'green', activeforeground = 'white', command = addData)
    save.place(x = 34, y = 573)
        
        
    # EDIT BUTTON
    edit = Button(window, text = "Edit", font = ('Arial', 12), padx = 45,  pady = 5, cursor = 'hand2', bg = 'orange', fg = 'white',
                    activebackground = 'orange', activeforeground = 'white', bd = 0)
    edit.place(x = 180, y = 573)
        
    # DELETE BUTTON
    delete = Button(window, text = "Delete", font = ('Arial', 12), padx = 45,  pady = 5, cursor = 'hand2', bg = 'red', fg = 'white',
                    activebackground = 'red', activeforeground = 'white', bd = 0)
    delete.place(x = 93, y = 648)
        
        
    # CONNECTION BUTTON
    connection_button = Button(window, text = "Check Stat", font = ('Arial', 12), padx = 10,  pady = 5, cursor = 'hand2', bg = 'blue', fg = 'white',
                    activebackground = 'blue', activeforeground = 'white', bd = 0, command = databaseConnection)
    connection_button.place(x = 1064, y = 648)
        
        
    # SEARCH LABEL
    search = Label(window, text = "Search", font = ('Arial', 12), bg = 'white')
    search.place(x = 878, y = 44)
        
        
    # SEARCH ENTRY
    search_entry = Entry(window, font = ('Arial', 12), width = 15, highlightbackground = 'gray', highlightcolor = 'black',
                                highlightthickness = 1, bd = 0)
    search_entry.place(x = 966, y = 41)



    # Create Treeview Frame
    tree_frame = Frame(window)
    tree_frame.pack(pady=20)

    # Treeview Scrollbar
    tree_scroll = Scrollbar(tree_frame)
    tree_scroll.pack(side=RIGHT, fill=Y)

    # Create Treeview
    my_tree = ttk.Treeview(tree_frame, yscrollcommand=tree_scroll.set, selectmode="extended")

    # Pack to the screen
    my_tree.pack(side= LEFT)

    #Configure the scrollbar
    tree_scroll.config(command=my_tree.yview)

    # Define Our Columns
    my_tree['columns'] = ("SRID", "fullname", "projects", "startofcontract", "endofcontract", "status")

    # Formate Our Columns
    my_tree.column("#0", width=0, stretch=NO)
    my_tree.column("SRID", anchor=W, width=140)
    my_tree.column("fullname", anchor=CENTER, width=100)
    my_tree.column("projects", anchor=W, width=140)
    my_tree.column("startofcontract", anchor=CENTER, width=140)
    my_tree.column("endofcontract", anchor=W, width=140)
    my_tree.column("status", anchor=CENTER, width=140)

    # Create Headings 
    my_tree.heading("#0", text="", anchor=W)
    my_tree.heading("SRID", text="SRID", anchor=W)
    my_tree.heading("fullname", text="FullName", anchor=CENTER)
    my_tree.heading("projects", text="Projects", anchor=W)
    my_tree.heading("startofcontract", text = "Start", anchor=W)
    my_tree.heading("endofcontract", text = "End", anchor=W)
    my_tree.heading("status", text = "Status", anchor=W)


        
    window.mainloop()

mainWindow()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文