我如何以一种方式编码,告诉我的用户输入在数据库中不可用?
这是我的代码:
def sql_getage(name):
query = """\
select age from store
where name = '{}'
""".format(name)
results = (execute_read_query (conn, query))
for i in results:
theresult = str(i[0])
return (theresult)
name = input("Please input your username")
age = sql_getage(name)
print("Your age is", age)
我的数据库有 2 列:
username Age
John 20
Amy 21
我如何编写代码,如果用户输入名称为 Jhn,并且在数据库表中找不到它,那么我会打印wrong username
目前如果我输入错误的名字,它仍然会继续下一个代码,打印年龄。它会打印一个空的年龄。
梵蒂冈之花
相关分类