简体   繁体   English

如何检查表是否包含数据? (Python Flask MYSQL)

[英]How do you check if a table contains data? (Python Flask MYSQL)

How exactly do you check if a table contains a specific value?您如何检查表是否包含特定值? For example,例如,

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```

The data variable here will return a tuple from the cur.fetchall() function which will look something like this这里的data变量将从cur.fetchall() function 返回一个元组,看起来像这样

((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')...)

If you need to find a row with a specific email address I suggest you do this for you SQL command如果您需要查找具有特定 email 地址的行,我建议您为您执行此 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

The data tuple will then only contain a single row from the SQL table as follows (1,'random_email1@mail.com','First_name','Last_name')然后,数据元组将仅包含 SQL 表中的一行,如下所示(1,'random_email1@mail.com','First_name','Last_name')

Here you can simply fetch the email address value by using data[1] which is where the email is in the tuple.在这里,您可以使用data[1]简单地获取 email 地址值,这是 email 在元组中的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你如何在python中检查一个字符串是否只包含数字? - How do you check in python whether a string contains only numbers? 如何在python中使用网址执行mysql包含的内容? - How do you perform mysql contains with a url in python? 如何序列化 python flask 变量? - how do you serialize a python flask variable? Python-如何填充mysql表的输出 - Python - How do you pad the output of a mysql table 如何使用Ajax将表中的数据发布到Flask视图? - How do you post data from a table using Ajax to a Flask view? 如何检查列表中是否包含字典? - How do you check whether a list contains a dictionary inside? 如何获得在 python/flask 中具有多个相同键值的 JSON 数据 - How do you get JSON data that has multiple of the same key values in python/flask Flask 和 MySQL,如何循环数据库查看用户是否在表中? - Flask and MySQL, how to loop through the database to check if a user is in the table? 如何将复选框值传递给 Python Flask 的模态引导程序从 mysql 表中删除数据? - How to pass checkbox values to modal bootstrap for Python Flask remove data from mysql table? 在Python中,当模板文件包含“ $”时如何进行替换? - In Python, how do you do substitutions when the template file contains a “$”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM