在if语句中包含一个SQLite INSERT INTO语句

时间:2019-06-13 18:21:03

标签: python sqlite tkinter

我似乎无法将记录发布到我的sqlite数据库中。

我的if语句不断出错:

from tkinter import *
import sqlite3

root = Tk()
root.title('Learn To Code')
root.geometry('300x300')

#Create a connection
conn = sqlite3.connect('address_book.db')

#Create a cursor
c = conn.cursor()

def addRecord():
    #Create a connection
    conn = sqlite3.connect('address_book.db')

    # Create a cursor
    c = conn.cursor()
    if entryFirstN is not None:
        c.execute('INSERT INTO addresses VALUES(:fName, :lName, :address, :city, :state, :zip)',
              {
                  'fName': entryFirstN.get(),
                  'lName': entryLastN.get(),
                  'address': entryAddress.get(),
                  'city': entryAddress.get(),
                  'state': entryState.get(),
                  'zip': entryZip.get()
              }
        )
    else:
       from tkinter import messagebox
       response = messagebox.showinfo('Error', 'Please input all information')


    # Commit Changes
    conn.commit()

    # Close Connection
    conn.close()

    # Clear the text boxes
    entryFirstN.delete(0, END)
    entryLastN.delete(0, END)
    entryAddress.delete(0, END)
    entryCity.delete(0, END)
    entryState.delete(0, END)
    entryZip.delete(0, END)


#Header
lblHeader = Label(root, text='Address Book').grid(row=0, column=0, 
columnspan=2)

#User Entry
entryFirstN = Entry(root, width=20)
entryFirstN.grid(row=1, column=1)
entryLastN = Entry(root, width=20)
entryLastN.grid(row=2, column=1)
entryAddress= Entry(root, width=20)
entryAddress.grid(row=3, column=1)
entryCity = Entry(root, width=20)
entryCity.grid(row=4, column=1)
entryState = Entry(root, width=20)
entryState.grid(row=5, column=1)
entryZip = Entry(root, width=20)
entryZip.grid(row=6, column=1)

#Button Add Record
btnAddRecord = Button(root, text='Add Record to Database', width=20, 
command=addRecord)
btnAddRecord.grid(row=7, column=0, pady=5, columnspan=2)

#Button Show Records
btnShowRecords = Button(root, text='Show Records', width=20)
btnShowRecords.grid(row=8, column=0, pady=5, columnspan=2)

#Commit Changes
conn.commit()

#Close Connection
conn.close()

root.mainloop()

当前,当文本框为空时,我没有收到消息框,它什么也没做。

相反,我需要程序检查以确保填写了名字输入文本框,如果是,则将数据粘贴到数据库中,如果不是,则应出现一个消息框。

0 个答案:

没有答案