简体   繁体   中英

Comparing user inputs to items within a list taken from a column of a database (Python3/SQL)

I'm trying to have a user input be compared with a list of values taken from a column in an Access database. The way I've tried to do this is to read each field under a specific column, append it to a list, and have that user input be compared against that list using "if value in list:". However, the function doesn't work as every time I try to compare against the list taken from the database, the result is always a False, even if the string is in that list.

I believe the cause of this is because Python takes the column of the database that I want and formats the list like this:

['('string1', )' , '('string2', )' , '('string3', )']

instead of how it's normally set out, like:

['string1','string2','string3']

which causes the "if value in list" function to fail when the value is a regular input.

Even if I try to manually concatenate the differences with the string to make everything as it is displayed in the list, the comparison still always produces the same output.

Here's my actual code:

 newentry = (username, password, mail)
                    unameCheck = []
                    mailCheck = []
                    unamesub = str("('" + username + "', )") #this is how I tried to concatenate the extra bits of string to match up with the entries in the list.
                    mailsub = str("('" + mail + "', )")
                    print (unamesub)
                    print (mailsub)
                    print(newentry)
                    connector =  pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\\Users\\User\\Documents\\My Folder\\db.accdb;')
                    cursor = connector.cursor()
                    for row in cursor.execute("SELECT Username FROM userdata"):
                        unameCheck.append(row)
                        print(unameCheck)

                    for row in cursor.execute("SELECT EmailAddress FROM userdata"):
                        mailCheck.append(row)
                        print(mailCheck)

                    if str(unamesub) in unameCheck or username == '':
                        messagebox.showerror("Invalid Username", "Invalid or blank Username")
                    else:
                        if str(mailsub) in mailCheck or mail == '':
                            messagebox.showerror("Invalid E-mail", "E-mail either blank or already registered, Log in or reset password using the 'Save Me!' option.")
                        else:

                            cursor.execute("INSERT INTO userdata VALUES " + str(newentry))
                            connector.commit()
                            controller.show_frame(menu)
                else:
                    messagebox.showerror("Invalid E-mail", "E-mail not confirmed, type the same E-mail into both of the required fields.")
            else:
                messagebox.showerror("Invalid Password", "Password not confirmed, type the same Password into both of the required fields.")

All of the unnecessary print() functions are just to check if variables are doing what they're meant to be doing.

Is there a method of either removing the extra brackets from around the strings in the SQL taken list or is there another way of comparing an input to an element within the table?

Thanks

For each iteration of …

for row in cursor.execute("SELECT Username FROM userdata"):

row will be a pyodbc.Row object, which is very similar to (but not the same as) a tuple. So, even though you are only selecting a single column the row object will be something like

('gord',)

Therefore, instead of using unameCheck.append(row) inside the loop, use unameCheck.append(row[0]) to produce a list of simple string (scalar) values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM