简体   繁体   English

使用sqlite3 db中的查询匹配用户输入

[英]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',) (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. DB提取模型为每一行返回一个元组。 Since you've only selected a single field, you can simply access namer[0] to get the actual value. 由于只选择了一个字段,因此只需访问namer[0]即可获取实际值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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