简体   繁体   中英

Matching user's input with query from sqlite3 db in python

I'm writing a script where by a user registers his/her username, but a function checks whether this username is already in the db or not. But I'm stuck on how to match my query with the input. Here is the code:

def checker(self, insane):                                                  
        t = (insane,)                                                           
    cmd = "SELECT admin_user FROM admin_db where admin_user = \"%s\";" %t
    self.cursor.execute(cmd)
    namer = self.cursor.fetchone()
    print namer
    if namer == insane:                                                     
        print("Username is already chosen!")
        exit(1)
    else:
        pass

Since

namer
returns as
 (u'maverick',)  
It doesn't match with the input. How should I go about implementing that?

The DB fetch models return a tuple for each row. Since you've only selected a single field, you can simply access namer[0] to get the actual value.

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