gpt4 book ai didi

python - 如何在 python mysql 中比较索引值与数据库值?

转载 作者:行者123 更新时间:2023-12-01 06:26:43 25 4
gpt4 key购买 nike

我正在做的是将索引值与数据库的bar_code_no进行比较,如果bar_code_no通过索引值进行验证,那么我想打印经过验证的bar_code_no的整行,其中我有姓名,电子邮件,电话号码。我尝试过,但无法进行比较。

import mysql.connector as mysql
mydb1=mysql.connect(
user = 'rajat',
passwd = 'rajat',
host = 'localhost',
database = 'master_database'
)
bal = []
validate = ('1', 'Green', '12:34:56:78:91:22', '456456', '2')
val = validate[3]
bal.append(val)
print(bal)

new_list = []
mycursor3 = mydb1.cursor()
mycursor3.execute("SELECT bar_code_no FROM android_display_data")
df = mycursor3.fetchall()
print(df)

if bal in df:
print('it exists')
else:
print('it does not exists')

输出是这样的:-

['456456']
[('456456',)]
it does not exists

最佳答案

我得到了如何验证值的答案,但仍然遇到一个问题,即如何打印存在经过验证的 bar_code_no 的整行。

import mysql.connector as mysql
mydb1=mysql.connect(
user = 'rajat',
passwd = 'rajat',
host = 'localhost',
database = 'master_database'
)
bal = []
validate = ('1', 'Green', '12:34:56:78:91:22', '456456', '2')
val = validate[3]
bal.append(val)
print(bal)

new_list = []
mycursor3 = mydb1.cursor()
mycursor3.execute("SELECT bar_code_no FROM android_display_data")
df = mycursor3.fetchall()
df1 = df[0]
df2 = list(df1)
print(df2)


if bal == df2:
print('its exists')
else:
print('it does not exist')

关于python - 如何在 python mysql 中比较索引值与数据库值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60108657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com