如何在python中将输入值与mysql数据库值进行比较 [英] How to compare input value with mysql database value in python

查看:135
本文介绍了如何在python中将输入值与mysql数据库值进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I want to compare the input value with my database value. IF the input value is the same value as the database, I want to print(inputvalue). But if it's not the same, I want to 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

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

If the Data Exist
Any answer would be appreciated!

推荐答案

您可以创建布尔对象并在执行后比较您的答案:

you can make boolean object and after executing compare your answer :

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

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

temp = False
for x in result:
    if inputvalue in x:
        temp = True
if temp:
    print(inputvalue)
else:
    print("Data Does Not Exist")

这篇关于如何在python中将输入值与mysql数据库值进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆