我正在做的是将索引值与数据库的bar_code_no进行比较,如果用索引值验证了bar_code_no,那么我要打印整行经过验证的bar_code_no,其中我有姓名,电子邮件,phone.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/