简体   繁体   中英

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. 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. And your query could return one record or nothing ( 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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