我想搜索數據庫的明天日期,然後進入電子郵件功能。在sqlite中的日期搜索不起作用
def send_email():
currentdate = time.strftime("%d/%m/%Y")
nextday = datetime.date.today() + datetime.timedelta(days=1)
tomorrow = str(nextday.strftime("%d %m %Y"))
with sqlite3.connect("school.db") as db:
cursor = db.cursor()
cursor.execute("SELECT DateIn FROM MusicLoan WHERE DateIn = ?",(tomorrow,))
row = cursor.fetchall()[0]
if str(row) == str(tomorrow):
cursor.execute("SELECT StudentID FROM MusicLoan WHERE DateIn = ?", (currentdate))
ID = cursor.fetcone()[0]
cursor.execute("SELECT email FROM Student WHERE StudentID = ?",(ID))
email = cursor.fetchone()[0]
fromaddr = email
toaddr = "[email protected]"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Music Reminder"
body = "A reminder that your music is due in tomorrow"
msg.attach(MIMEText(body, 'plain'))
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.set_debuglevel(1)
server.starttls()
server.ehlo()
server.login("[email protected]", "DanR0bJ0nes3")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
print("Email Sent Successfully")
send_email()
except smtplib.SMTPException:
print ("Error: unable to send email")
send_email()
我的問題是,它得到行= cursor.fetchall()[0],然後停止。 我該如何解決這個問題
我沒有注意到那裏的邏輯錯誤,我很快寫下這個錯誤。它已被編輯。 如何迭代這些結果? –
使用for循環:'for cursor in cursor.fetchall()',然後'if row [0] == str(tomorrow)',儘管修改過的代碼永遠不會是錯誤的。 –
改變了一些東西,現在它的工作原理,謝謝:) –