Python中出生日期的格式检查
我目前正在开发一个数据库应用程序,用户在其中填写客户表单,然后将数据添加到SQLite3表中。我正在尝试将验证添加到代码中,并使其正常工作,但它将记录添加到包含无效数据的表中。如果数据无效,有没有办法停止代码的执行?请参见下面的代码和界面截图:Python中出生日期的格式检查,python,database,sqlite,validation,tkinter,Python,Database,Sqlite,Validation,Tkinter,我目前正在开发一个数据库应用程序,用户在其中填写客户表单,然后将数据添加到SQLite3表中。我正在尝试将验证添加到代码中,并使其正常工作,但它将记录添加到包含无效数据的表中。如果数据无效,有没有办法停止代码的执行?请参见下面的代码和界面截图: def validate(self): dateofbirth = self.DOBEntry.get() try: datetime.datetime.strptime(dateofbir
def validate(self):
dateofbirth = self.DOBEntry.get()
try:
datetime.datetime.strptime(dateofbirth, '%D/%M/%Y')
except ValueError:
tkinter.messagebox.showerror("Error","Please enter the date of birth in the correct format DD-MM-YYYY")
def AddCustomer(self):
customerid = int(self.CustomerEntry.get())
branchid = self.BranchEntry.get()
name = self.NameEntry.get()
surname = self.SurnameEntry.get()
dateofbirth = self.DOBEntry.get()
town = self.TownEntry.get()
postcode = self.PostcodeEntry.get()
email = self.EmailEntry.get()
telephone = self.TelephoneEntry.get()
medical = self.MedicalEntry.get()
try:
self.validate()
with sqlite3.connect("LeeOpt.db") as db:
cursor = db.cursor()
add_customer = ('''INSERT INTO Customer(CustomerID, BranchID, Name, Surname, DateOfBirth, Town, Postcode, EmailAddress, TelephoneNo, MedicalConditions)
VALUES (?,?,?,?,?,?,?,?,?,?)''')
cursor.execute(add_customer, [(customerid),(branchid),(name),(surname),(dateofbirth),(town),(postcode),(email),(telephone),(medical)])
tkinter.messagebox.showinfo("Notification","Customer added successfully")
self.ClearEntries()
except (sqlite3.IntegrityError):
tkinter.messagebox.showerror("Error","This CustomerID is already taken, please try another.")
self.ClearID()
试试这样的方法:
def validate(self):
dateofbirth = self.DOBEntry.get()
try:
datetime.datetime.strptime(dateofbirth, '%D/%M/%Y')
return True # returns a boolean True
except ValueError:
tkinter.messagebox.showerror("Error","Please enter the date of birth in the correct format DD-MM-YYYY")
return False
def AddCustomer(self):
# Same code
if self.validate(): # If true is returned, then...also same as if self.validate()==True
try:
with sqlite3.connect("LeeOpt.db") as db:
cursor = db.cursor()
add_customer = ('''INSERT INTO Customer(CustomerID, BranchID, Name, Surname, DateOfBirth, Town, Postcode, EmailAddress, TelephoneNo, MedicalConditions)
VALUES (?,?,?,?,?,?,?,?,?,?)''')
cursor.execute(add_customer, (customerid,branchid,name,surname,dateofbirth,town,postcode,email,telephone,medical))
tkinter.messagebox.showinfo("Notification","Customer added successfully")
self.ClearEntries()
except (sqlite3.IntegrityError):
tkinter.messagebox.showerror("Error","This CustomerID is already taken, please try another.")
self.ClearID()
在这里,只有当行中没有错误时才会返回
True
,否则将返回False
,因此在此基础上,您在AddCustomer()内创建if
if
。我还将execute
的参数更改为元组,而不是常规的列表。什么是无效数据?只需让validate
返回True
或False
,然后在更新记录之前检查该值即可。或者,让validate
在显示对话框后重新引发异常。@CoolCloud一个不符合函数中设置的格式的日期。我认为将self.validate
放在try
中没有好处,因为您跳过了函数中的错误,因此不会显示任何错误。因此,我认为最好从函数返回True
或False
,并检查是否为self.validate():
,然后继续。@CoolCloud请原谅我的无知,我是Python初学者,如何返回True
或False
?。我做了一些研究,但不能理解它。我尝试了这个,但是,它不断检测到错误并显示错误消息,即使日期格式输入正确。@arthur_fleck日期错误消息?如果出现这种情况,则表示存在ValueError
,请在函数中使用print(dateofbirth)
,并确保数据符合预期。