简体   繁体   中英

How can I create a multi-value dictionary?

I have user info from a SQL Server - a username and password - and now I want to create a dictionary

{ID : password}

but if I have more then one it just saves the last one I put in

for i in (cursor.execute('select * from Person')):
    idNameDict = {i[0]:i[3]}

I want to do it this way so it would be easier for me to do check if the based on the ID that the password input would be correct because right now and it seems to long

def logIn(userName,userPassword):
    userID=[]
    global p
    lbl = Label(root)
    lbl.grid(row=5,column=1)
    for user in (cursor.execute('select * from Person')):
        p = Person(user[0],user[1],user[2],user[3])
        userID.append(p)
    for id in userID:
        if userName == id.ID:
            if userPassword == id.password:
                userPage(root,userName)
            else:
                lbl.config(text= "Invalid Password")
        else:
            lbl.config(text= "Invalid User Name")

Your current code doesn't work properly because you declare the dictionary from scratch in each iteration. You need to do it in the following way:

idNameDict = {}
for i in (cursor.execute('select * from Person')):
    idNameDict[i[0]] = i[3]

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