2017-09-06 29 views
1
def displayResult(self): 
    name = input("> ") 
    self.cursor.execute("SELECT username FROM customer Where username = '%s' " % name) 
    if name in self.cursor.fetchone(): 
     print("flag") 
    elif name not in self.cursor.fetchone(): 
     print("Not in here.") 
    elif name == "" or name == " ": 
     print("Input something.") 

私は次の機能を使用しようとしています:ユーザ名をデータベースに持っていればユーザ名を入力すると 'flag'何も入力されていない場合は、「ここには印刷されません」 このコードを実行して、ユーザー名を入力して空きスペースを残そうとすると、このエラーが発生します。TypeError:タイプ 'NoneType'の引数がmysql.connectorで反復できません

私は

おかげで012警告このエラーを遮断することができる方法

TypeError: argument of type 'NoneType' is not iterable 

答えて

0

例えば、それを反復処理しようとする前にfetchoneの戻り値のチェックをやってみてください。

one = self.cursor.fetchone() 
if one is not None: 
    if name in one: 
     print("flag") 
    # etc 
+0

私はおかげで、これを得たが、私はまだ理解していない、self.cursor.fetchone()値はNoneですなぜ? – lostage

+0

[このメソッドは、クエリ結果セットの次の行を取得し、単一のシーケンスを返します。使用可能な行がない場合はNoneを返します。](https://dev.mysql.com/doc/connector-python/en/connector- python-api-mysqlcursor-fetchone.html)。 – 101

関連する問題