从Microsoft SQL Server管理数据库切换到Snowflake后,Python应用程序不发送电子邮件

dtcbnfnu  于 2023-02-11  发布在  SQL Server
关注(0)|答案(1)|浏览(144)

我构建了一个基于Python的应用程序,它连接到Microsoft SQL Server管理数据库,并向用户发送登录详细信息。
但是,我们最近从Microsoft SQL Server管理更改为Snowflake,无法完全正常工作。
我能够连接到 snowflake 数据库并通过帐户进行搜索。只有当我点击应该触发电子邮件发送的按钮时,控制台才会产生错误。
下面是应该触发电子邮件的函数的代码:

# Send login details button
def send_logins(retailer):
    c.execute("""SELECT Retailer, Retailer_Name, Contact, E_Mail, Account_ID, Password FROM Retailers WHERE Retailer = ? """, (retailer[0]))
    users = c.fetchall()
    # Create a secure SSL context
    context = ssl.create_default_context()
    with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
        try:
            server.login(sender_email, password)
        except SMTPAuthenticationError:
            print("Username and/or password you entered is incorrect")
        try:
            missing = []
            for Retailer, Retailer_Name, Contact, E_Mail, Account_ID, Password in users:
                valid = True
                if not Retailer_Name:
                    missing.append("Business has no name!")
                    valid = False
                if not Account_ID:
                    missing.append("Business has no username!")
                    valid = False
                if not Password:
                    missing.append("Business has no password!")
                    valid = False
                if not E_Mail:
                    missing.append("Business has no email address!")
                    valid = False
                if not Contact:
                    Contact = ' '
                    valid = True
                if valid:
                    server.sendmail(
                            sender_email,
                            E_Mail.split(';'),
                            message.as_string().format(
                                Retailer_Name=Retailer_Name,
                                Contact=Contact,
                                E_Mail=E_Mail,
                                previous_month=previous_month,
                                year=year,
                                Account_ID=Account_ID,
                                Password=Password,
                                comp_logo=comp_logo,
                                comp_twitter=comp_twitter,
                                comp_linkedin=comp_linkedin,
                                comp_insta=comp_insta),
                    )
                    server.sendmail(
                            sender_email,
                            E_Mail.split(';'),
                            message2.as_string().format(
                                Retailer_Name=Retailer_Name,
                                Contact=Contact,
                                E_Mail=E_Mail,
                                previous_month=previous_month,
                                year=year,
                                Account_ID=Account_ID,
                                Password=Password,
                                comp_logo=comp_logo,
                                comp_twitter=comp_twitter,
                                comp_linkedin=comp_linkedin,
                                comp_insta=comp_insta),
                    )
                    if valid:
                        print("Emails sent to " + str(Contact) + " at " + str(Retailer_Name))
                        sent_outcome_label.config(text="Emails sent to " + str(Contact) + " at " + str(Retailer_Name))
                        missing_fields_label.config(text="")
                else:
                    print("Emails not sent to " + str(Retailer_Name) + "!")
                    sent_outcome_label.config(text="Emails not sent to " + str(Contact) + " at " + str(Retailer_Name) + ":")
                    missing_fields_label.config(text=("\n".join(missing)))
                    print(missing)
        except SMTPException as e2:
            print(e2)
        except Exception as e:
            print("Emails not sent!")
            print(e)

应出现在SELECT语句中的数据示例:
| 零售商|零售商_名称|接触|电子邮件|科目_ID|密码|
| - ------|- ------|- ------|- ------|- ------|- ------|
| 一千|商业一号|鲍勃|Example@example.com|一千|吉235 G|
| 千零一|商务二|罗伯|Example1@example.com|千零一|tFz145f|
控制台中显示的错误:

File "C:\Users\Person\OneDrive - Company\Documents\Python\Comp Interface\sil_sf_resend_app.py", line 599, in <lambda>                                                                                                                                
    send_login_button = ttk.Button(frame_send_logins, text="Yes", width=10, command=lambda: send_logins(retailer))                                                                                                                                                    
  File "C:\Users\Person\OneDrive - Company\Documents\Python\Comp Interface\sil_sf_resend_app.py", line 380, in send_logins                                                                                                                             
    c.execute("""SELECT Retailer, Retailer_Name, Contact, E_Mail, Account_ID, Password FROM Retailers WHERE Retailer = ? """, (retailer[0]))                                                                                                                          
  File "C:\Users\Person\AppData\Local\Programs\Python\Python39\lib\site-packages\snowflake\connector\cursor.py", line 660, in execute                                                                                                                                  
    if params is not None and len(params) == 0:                                                                                                                                                                                                                       
TypeError: object of type 'int' has no len()

我有一种感觉,这是一些与c.执行语句和?占位符,但没有运气在所有!
我试图包括尽可能多的信息,而不暴露任何敏感信息,但请让我知道,如果有我的代码,你想看到的其他部分。

ff29svar

ff29svar1#

因此,在@acw1668的帮助下,我不得不将SELECT语句从:

c.execute("""SELECT Retailer, Retailer_Name, Contact, E_Mail, Account_ID, Password FROM Retailers where Retailer = ? """, (retailer[0]))

致:

c.execute("""SELECT Retailer, Retailer_Name, Contact, E_Mail, Account_ID, Password FROM Retailers WHERE Retailer = %s """, [retailer[0]])

注意'retailer'两边括号中的变化,这是因为c.execute()的第二个参数需要一个元组或列表。
然后我还发现Snowflake中的占位符与SQL中的占位符工作方式不同。我不得不将"?"替换为" % s"

相关问题