Skip to content

Instantly share code, notes, and snippets.

@ayancey
Created Feb 14, 2017
Embed
What would you like to do?
import records
import datetime
db = records.Database('mysql://root:a6478a14@172.16.144.157/bank')
print('Welcome to Softbank. Please enter your username and password...')
username = raw_input('Username: ')
password = raw_input('Password: ')
# This SQL library does not immediately run the query and store the results until we tell it to.
# In our case, this is not very helpful, but when running a query with hundreds of thousands of rows,
# we may not want to immediately load them all into RAM
result = db.query("SELECT * FROM user WHERE username = '" + username + "' AND password = '" + password + "';")
# Load all rows from query
result_data = result.all()
# Returns the number of rows returned from the query
rows = len(result_data)
success = "0"
if rows > 0:
print 'Welcome ' + result_data[0]['first_name'] + '!'
success = "1"
else:
print 'Invalid username and password combination. Please try again.'
# We need to run an extra query searching with just the username, in case the user entered the password incorrectly,
# then we can still select the username for adding a failed login attempt
# Using two SQL queries would probably be bad practice...you would probably just use a string comparison between the username
# and password entered
username_id = db.query("SELECT * FROM user WHERE username = '" + username + "';").all()
if username_id:
# Get the current time from the system clock, and convert it to the MySQL datetime style
current_time = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
db.query("INSERT INTO login VALUES (NULL, " + str(username_id[0]['id']) + ", '" + current_time + "', " + success + ");")
# Add the login attempt to the login table. This will add all successful and failed attempts to log in with this username
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment