如何打印fetchone()函数但得到NoneType错误? SQLITE3

时间:2017-11-01 14:03:47

标签: python sqlite

我正在尝试打印fetchone()值,但它只是给我错误'NoneType' object is not iterable,有人知道为什么

这是我的代码:

def viewemaillook():
        global realusername
        global realpassword
        viewemail = input("Would you like to see/change your email")
        if viewemail == "y" or viewemail == "yes":
            c.execute("SELECT email, * FROM stuffToPlot WHERE username = ?  and password = ?", (realusername,realpassword,))
            grab = c.fetchone()
            for i in grab:
                print(i)
        elif viewemail == "n" or "no":
            print("Okay")
        else:
            print("Invalid option")
            viewemaillook()
    viewemaillook()
else:
    print("Not a valid option")
    change()
change()

1 个答案:

答案 0 :(得分:1)

如果您的查询未找到任何结果,则fetchone()将返回None。当您尝试循环等于grab的{​​{1}}时,您将收到错误消息。请先检查None是否有结果。