简体   繁体   English

SQLite3数据库Python条件语句

[英]Sqlite3 database Python conditional statement

import sqlite3
conn = sqlite3.connect('LeVinEmployee.db')
profile = input("Are you a user? y/n: ")
if profile == 'y':
    login = input("Enter login name: ")
    #passw = input("Enter password: ")
    c = conn.cursor()
    c.execute("SELECT * FROM Employee WHERE Email = '" + login + "'")
    result = c.fetchone()
    if result[0] == 1:
         print(c.fetchall())
    else:
         print("not")

else:
    print("You are not a user")

What I am trying to do here is pretty simple. 我在这里要做的很简单。 I am trying to create user login function. 我正在尝试创建用户登录功能。 If user type 'y', program will simply ask to put login email. 如果用户键入“ y”,程序将只要求输入登录电子邮件。 If user type correct email from database, print that customer information. 如果用户键入来自数据库的正确电子邮件,请打印该客户信息。 if wrong, print 'not'. 如果输入错误,则打印“不”。 I am not sure what is wrong with my code. 我不确定我的代码有什么问题。 Can someone help me please? 有人能帮助我吗?

As I understand, email is an unique field. 据我了解, email是一个独特的领域。 And your query could return one record or nothing ( None ). 而且您的查询可能返回一条记录或不返回任何记录( None )。 Try 尝试

result = c.fetchone()
if result:  # result could be None or tuple (record)
     print(result)
else:
     print("not")

Also, such method of parameter pass is incorrect (insecure) 另外,这种参数传递方法不正确(不安全)

c.execute("SELECT * FROM Employee WHERE Email = '" + login + "'")

use 采用

c.execute("SELECT * FROM Employee WHERE Email=?", login)

more info here . 更多信息在这里

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

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