0

您如何检查表是否包含特定值?例如,

if request.method == 'POST':
        email = request.form.get("email")
        cur = mysql.connection.cursor()
        cur.execute("SELECT * from login_info")
        data = cur.fetchall()    
        if email == data: # <--- I don''t know. This is what I want to know```
4

1 回答 1

0

这里的data变量将从cur.fetchall()函数返回一个元组,看起来像这样

((1,'random_email1@mail.com','First_name1','Last_name1'), 
 (2,'random_email2@mail.com','First_name2','Last_name2'),
 (3,'random_email3@mail.com','First_name3','Last_name3')...)

如果您需要查找具有特定电子邮件地址的行,我建议您为您执行此 SQL 命令

email_query = "SELECT * FROM login_info WHERE email = %s"
values = (email)
cur.execute(email_query, values)
data = cur.fetchone()
#Assuming that there will be only one Row with the E-mail ID as the PRIMARY KEY

然后数据元组将只包含 SQL 表中的一行,如下所示 (1,'random_email1@mail.com','First_name','Last_name')

在这里,您可以简单地使用data[1]which is where the email is in the tuple 来获取电子邮件地址值。

于 2021-05-14T15:00:50.693 回答