简体   繁体   English

链接 tkinter 和 sqlite3

[英]Linking tkinter and sqlite3

I'm a new python programmer and I as creating a phonebook app using tkinter and sqlite3 but I faced a problem.我是一个新的 python 程序员,我正在使用 tkinter 和 sqlite3 创建一个电话簿应用程序,但我遇到了一个问题。 The app runs without problem but not inserting or deleting data although it prints (inserted successfully) and deleted successfully).该应用程序运行没有问题,但没有插入或删除数据,尽管它打印(成功插入)并成功删除)。 Please for you kind assistance.请为您提供帮助。 The code is below and thanks in advance:代码如下,提前致谢:

from tkinter import*
import tkinter as tk
from tkinter import ttk 
import sqlite3

class Phonebook2:   
def __init__(self, master):

    lblName = ttk.Label(top, text="Name:")
    lblName.grid(row = 0, column = 0, sticky=W)
    lblPhone = ttk.Label(top, text="Phone number:")
    lblPhone.grid(row=1, column=0, sticky=W)

    self.name= StringVar()
    self.txtName = Entry(top, textvariable=self.name)
    self.txtName.grid(row=0, column=1, padx=0, pady=0, sticky=EW, columnspan=19)

    self.num= IntVar()
    self.txtPhone = Entry(top, textvariable=self.num)
    self.txtPhone.grid(row=1, column=1, sticky=EW)


    btnAdd = ttk.Button(top, text="Add", command=self.insert)
    btnAdd.grid(row=0, column=20, sticky=EW, padx=2, pady=2)
    btnDelete = ttk.Button(top, text="Delete", command=self.delete)
    btnDelete.grid(row=1, column=20, sticky=EW, padx=2)
    btnClear = ttk.Button(top, text="Clear", command=self.clear)
    btnClear.grid(row=2, column=20, sticky=EW, padx=2)
    btnSearch = ttk.Button(top, text="Search")
    btnSearch.grid(row=3, column=20, sticky=EW, padx=2)
    btnExit = ttk.Button(top, text="Exit", command = exit)
    btnExit.grid(row=4, column=20)


def exit(self):
    top.destroy()


def insert(self):

    NM = self.txtName.get()
    PH = self.txtPhone.get()
    conn = sqlite3.connect ('Phonebook.db')
    print ("connection opened for insertion...")
    cursor = conn.cursor()
    cursor.execute ("""INSERT INTO Phonebook (NAME, PHONE_NUMBER) VALUES (?, ?)""", (NM, PH))
    conn.commit()
    conn.close()
    print ('Inserted successfully')


def delete(self):
    conn = sqlite3.connect('Phonebook.db')
    print ("Connection opened for deletion...")
    cursor = conn.cursor()
    cursor.execute ("DELETE FROM Phonebook")
    conn.commit()
    conn.close()
    print ("All records are deleted successfully...")


def clear(self):
    self.txtName.delete(0, END)
    self.txtPhone.delete(0, END)


top = Tk()
top.overrideredirect(True)
top.eval('tk::PlaceWindow . center')
top.title("PhoneBook2")
application = Phonebook2(top)

top.mainloop()

You could try to INSERT like this:您可以尝试像这样插入:

query = ("INSERT INTO Phonebook (NAME, PHONE_NUMBER)""VALUES (%s, %s)")
    data = (NM,PH)
    cursor.execute(query, data)
    connection.commit()

Be sure to get the correct names of columns "NAME" and "PHONE_NUMBER" in your sqlite确保在 sqlite 中获取正确的列“NAME”和“PHONE_NUMBER”名称

def del_something(): conn = sqlite3.connect("Phonebook.db") cur = conn.cursor() cur.execute("DELETE FROM phonebook WHERE oid = " + del_ent.get()) # <- change entry del_ent.delete(0, END) # clear entry conn.commit() conn.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM