簡體   English   中英

來自 tkinter 的數據操作問題

[英]Trouble with data manipulation from tkinter

問題
我無法在 tkinter 的 mysql 表上操作(插入、刪除、更新)數據。編輯:我一直在嘗試使用 tkinter 在 mysql 表上插入/更改數據。雖然運行此代碼時沒有錯誤,但我看不到任何更改在 mysql 的表格中制作。
代碼

def delete_record():
    code=tcode.get('1.0',END)

    #DATABASE CONNECTION
    if code.isdigit()==True:
        import mysql.connector as sqltor
        mycon=sqltor.connect(host="localhost",user="root",passwd=" ",database="iv")
        tkcursor=connection.cursor(prepared=True)
        tkcursor.executed("delete from salesperson where code=%s,(code)")
        mycon.commit()
        tkinter.messagebox.showinfo("Record Deleted")
        tkcursor.close()

code不會像你想象的那樣,也永遠不會通過isdigit測試。 因為您正在訪問END索引,所以數據將有一個尾隨換行符,因為 tkinter 小部件保證始終有一個尾隨換行符。

當您想要從文本小部件中准確獲取用戶輸入的內容時,您需要使用“end-1c”(“end”減去一個字符)。 那,或者在你得到它之后去掉尾隨的換行符。

code = tcode.get('1.0', 'end-1c')

調試的第一步應該始終是檢查變量以查看它們是否與您假設的一樣。

您的兩個錯誤是將get()Text小部件一起使用,並且您的 sql 命令似乎不正確。 所以試試這個:

def delete_record():
    code=tcode.get('1.0','end-1c') #change made here

    #DATABASE CONNECTION
    if code.isdigit()==True:
        import mysql.connector as sqltor
        mycon=sqltor.connect(host="localhost",user="root",passwd=" ",database="iv")
        tkcursor=connection.cursor(prepared=True)
        tkcursor.execute("delete from salesperson where code=%s",(code,)) #also change made here
        mycon.commit()
        tkinter.messagebox.showinfo("Record Deleted",'The records have been successfully deleted') #also addded message into the messagebox
        tkcursor.close()

在您的代碼之上執行所有導入也是一個好習慣。 為什么要使用end-1c Bryan Oakley 已經在下面進行了解釋,看看那里。 你沒有收到任何錯誤,因為if語句從未執行過,所以還要確保說出print(code)並確保它是一個數字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM