[英]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.