pymysql不返回任何东西

时间:2018-08-11 13:53:42

标签: python mysql python-3.x pymysql

我正在尝试创建一个脚本,该脚本将检查是否有今天要发生的事件,然后它将发送邮件。不幸的是,我被一些愚蠢的东西困住了。我不知道为什么我的代码在应有的情况下不从数据库返回任何内容。

# Enviar correu Python 3.6 (NO ANOMENAR EMAIL.PY)
import smtplib, pymysql.cursors, secrets, re, datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from time import strftime

def dbb():
    connection = pymysql.connect(host='X',
                                 user='root',
                                 password='X',
                                 db='deuresc',
                                 charset='utf8mb4',
                                 cursorclass=pymysql.cursors.DictCursor)
    try:
        with connection.cursor() as cursor:
            # Llegim tots els recordatoris
            dataactual = strftime('%Y-%m-%d')
            data = datetime.datetime.strptime(dataactual, "%Y-%m-%d")
            datafinal = data + datetime.timedelta(days=1)
            sql = "SELECT * FROM `deures` WHERE `data`=%s"
            cursor.execute(sql, datafinal)
            resultat = cursor.fetchall()

            for row in resultat:
                data = str(row['data'])
                print(data)
                tasca = str(row['tasca'])
                print(tasca)

            fromaddr = "X"
            PASSWORD = "X"

            welcome = ['Jefe', 'Gery', 'Boss', 'Gary']

            # Misstage del correu
            SUBJECT = "Recordatori deures " + data
            body = "Bon dia " + secrets.choice(welcome) + ',' +  '\n' + 'Tens les següents tasques programades per demà: ' + '\n' + data + ' ' + tasca
            toaddr = "X"

            try:
                msg = MIMEMultipart()
                msg['From'] = fromaddr
                msg['To'] = toaddr
                msg['Subject'] = SUBJECT

                msg.attach(MIMEText(body, 'plain'))

                # Detalles del servidor de correu (Gmail, SMTP, Port: 587, TLS)
                server = smtplib.SMTP('smtp.gmail.com', 587)
                server.starttls()
                server.login(fromaddr, PASSWORD)
                text = msg.as_string()
                server.sendmail(fromaddr, toaddr, text)
                server.quit()
                print("S'ha enviat el recordatori a '" + toaddr + "' satisfactoriament!")
            except:
                print("S'ha produït un error!")
    finally:
        connection.close()

dbb()

当前数据库行:

Image

新代码工作已编辑

0 个答案:

没有答案