【发布时间】:2019-11-30 18:13:42
【问题描述】:
当我在 tkinter 上单击添加时,它不会在数据库中添加任何内容 我不太了解sqlite3。
我的数据库文件:
import sqlite3
from sqlite3 import *
conn = sqlite3.connect("connects.db")
print("Opened database successfully")
cursor = conn.cursor()
def createTable():
conn.execute('''CREATE TABLE IF NOT EXISTS contactlist
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
NAME TEXT NOT NULL,
PHONE INT NOT NULL);''')
print ("Table created successfully")
cursor.close()
conn.commit()
conn.close()
Python 文件:
from tkinter import *
from tkinter import messagebox
import os
from DatabaseFile import *
import sqlite3
def selection () :
print ("At %s of %d" % (select.curselection(), len(contactlist)))
return int(select.curselection()[0])
def addContact() :
conn = sqlite3.connect('connects.db')
cursor = conn.cursor()
entry = (nameVar.get(), phoneVar.get())
try:
def insertContact(entry):
conn.execute('INSERT INTO contactlist (NAME, PHONE) VALUES (?,?)', (entry))
conn.commit()
print('record inserted in data')
except:
messagebox.showwarning("Field cannot be blank","Please enter a value in both the fields.")
else:
setList()
def updateContact() :
contactlist[selection()]=[nameVar.get(), phoneVar.get()]
setList ()
def deleteContact() :
del contactlist[selection()]
setList ()
def loadContact () :
name, phone = contactlist[selection()]
nameVar.set(name)
phoneVar.set(phone)
def saveContact () :
fobj = open("contacts.py", "w")
fobj.write("contactlist = [")
for items in contactlist:
fobj.write(str(items))
fobj.write(",")
fobj.write("\n")
fobj.write("]")
def exitContact () :
app_title = "Contacts"
if (messagebox.askokcancel(title = app_title, \
message = "Do you want to exit, OK or Cancel") == 1) :
os._exit(1)
def buildFrame () :
global nameVar, phoneVar, select
root = Tk()
root.title('My Contact List')
frame1 = Frame(root)
frame1.pack()
Label(frame1, text="Name:").grid(row=0, column=0, sticky=W)
nameVar = StringVar()
name = Entry(frame1, textvariable=nameVar)
name.grid(row=0, column=1, sticky=W)
Label(frame1, text="Phone:").grid(row=1, column=0, sticky=W)
phoneVar= StringVar()
phone= Entry(frame1, textvariable=phoneVar)
phone.grid(row=1, column=1, sticky=W)
frame1 = Frame(root) # add a row of buttons
frame1.pack()
btn1 = Button(frame1,text=" Add ",command=addContact)
btn2 = Button(frame1,text="Update",command=updateContact)
btn3 = Button(frame1,text="Delete",command=deleteContact)
btn4 = Button(frame1,text=" Load ",command=loadContact)
btn5 = Button(frame1,text=" save ",command=saveContact)
btn6 = Button(frame1,text=" exit ",command=exitContact)
btn1.pack(side=LEFT); btn2.pack(side=LEFT)
btn3.pack(side=LEFT); btn4.pack(side=LEFT)
btn5.pack(side=LEFT); btn6.pack(side=RIGHT)
frame1 = Frame(root) # allow for selection of names
frame1.pack()
scroll = Scrollbar(frame1, orient=VERTICAL)
select = Listbox(frame1, yscrollcommand=scroll.set, height=7)
scroll.config (command=select.yview)
scroll.pack(side=RIGHT, fill=Y)
select.pack(side=LEFT, fill=BOTH)
return root
def setList() :
global contacts
contacts = readContacts()
# sort the list
contacts.sort(key = lambda x : x[1])
# delete all elements from the select element
select.delete(0, END)
# insert each name from the list to the end of the # select
# element
for id, name, phone in contacts :
select.insert(END, name)
def readContacts():
contactlistss = []
cursor.execute("SELECT * FROM contactlist")
rows = cursor.fetchall()
for row in rows:
contactlistss = row
return contactlistss
# the main program
# initialize the application by building the GUI elements
root = buildFrame()
# initialize the database
createTable()
# set the contents of the list initially
setList()
to keep the program from exiting
root.mainloop()
# end of program
当点击 tkinter 上的添加按钮时,它给了我空白输出,这意味着它没有在数据库中插入任何东西
【问题讨论】:
-
请尝试制作一个minimal的代码示例。这将对我们有所帮助,也许您可以自己找到解决方案。你也可以看看How to debug a program
-
只需要关于 addContact() 函数的帮助,它不向数据库输入任何输入
-
您是否收到错误消息?您应该始终使用
except Exception as ex: print(ex)来查看您遇到了什么异常 - 这样您就可以看到可能是什么问题。 -
您有问题,因为您定义了函数
def insertContact(entry):,但您从未运行它。不知道你为什么要定义这个函数——你应该直接把代码放在try/except