【问题标题】:How to compare input value with mysql database value in python如何在python中将输入值与mysql数据库值进行比较
【发布时间】:2023-04-07 04:36:01
【问题描述】:

所以我想将输入值与我的数据库值进行比较。如果输入值与数据库的值相同,我想print(inputvalue)。但如果不一样,我想print("Data Does Not Exist")

所以我试过这段代码:

cur = connection.cursor()
query = """ SELECT * FROM `foo` """

cur.execute(query)
result = cur.fetchall()
inputvalue = input("Input= ")

for x in result:
    if inputvalue not in x:
        print("Data Does Not Exist")
    else:
        print(inputvalue)

这是输出:

inputvalue= 1728192
Data Does Not Exist
Data Does Not Exist
Data Does Not Exist
Data Does Not Exist
1728192
Data Does Not Exist
Data Does Not Exist
Data Does Not Exist

我希望输出是

Inputvalue= 1728192
Data Does Not Exist

如果数据不存在
这个输出:

Inputvalue= 1728192
1728192

如果数据存在
任何答案将不胜感激!

【问题讨论】:

    标签:
    python
    python-3.x
    mysql-python