Search code examples
pythonmysqlcomparison

How to compare index value with database values in python mysql?


What I am doing is comparing a index value with the bar_code_no of database, if the bar_code_no is validated with the index value then I want to print the whole row of validated bar_code_no where I have name, email, phone.no. I tried it but the comparison is not possible.

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')

The output is like this:-

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

Solution

  • I got the answer of how to validate the values but still I got a problem that how to print the whole row in which the validated bar_code_no is present.

    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')