1

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
8
  • why not just do df = mycursor3.fetchall("SELECT bar_code_no FROM android_display_data"), that way you can delete the extra line of "executing" Commented Feb 7, 2020 at 7:09
  • I tried sir, but I dont know why I am getting stuck, as this is not a big problem. Commented Feb 7, 2020 at 7:10
  • df = mycursor3.fetchall("SELECT bar_code_no FROM android_display_data") using this it shows a error --TypeError: fetchall() takes 1 positional argument but 2 were given Commented Feb 7, 2020 at 7:14
  • 1
    ok...I see, what I will suggest is doing a SELECT * FROM ... WHERE "bar_code_no" = bal (which is the value at index 3 in validate). you will have to make a change to the sql query which is one way of doing it, the * (star) gets you all the records, and you use WHERE as the depending condition. Commented Feb 7, 2020 at 7:14
  • could fix the init to include an object for fetchall() or just change the class to @staticmethod, but I think in your case don't worry about the fetchall() just use execute() for now Commented Feb 7, 2020 at 7:21

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.