4
\$\begingroup\$

This is a registration and login script I have made in Python 3. It uses a MySQL database. In the future I might use it with my Blackjack game and add a row called money, but for now I would like to hear your opinion about this script since I have little to no experience in SQL.

import cymysql
from getpass import getpass
def get_user_info():
 while True:
 email = input("Input your Email address (max. 64 chars.): ")
 password = getpass("Input a password (max. 64 chars.): ")
 if len(email) < 64 and len(password) < 64:
 return email, password
def register(cur, email, password):
 cur.execute("INSERT INTO `users` (`Email`, `Password`) VALUES (%s, %s)", (email, password))
 print("You've succesfully registered!")
def login(cur, email, password):
 cur.execute("SELECT * FROM `users` WHERE `Email`=%s AND `Password`=%s LIMIT 1", (email, password))
 rows = cur.fetchall()
 if rows:
 print("You've succesfully logged-in!")
 else:
 print("You failed logging-in!")
def check_account(cur, email):
 cur.execute("SELECT * FROM `users` WHERE `Email`=%s LIMIT 1", (email,))
 row = cur.fetchone()
 return row
def main():
 conn = cymysql.connect(
 host='127.0.0.1',
 user='root',
 passwd='',
 db='david'
 )
 cur = conn.cursor()
 email = ''
 password = ''
 email, password = get_user_info()
 check = check_account(cur, email)
 if check:
 login(cur, email, password)
 else:
 register(cur, email, password)
 cur.close()
 conn.close()
if __name__ == '__main__':
 main()
esote
3,8002 gold badges24 silver badges44 bronze badges
asked Mar 23, 2019 at 17:58
\$\endgroup\$

1 Answer 1

5
\$\begingroup\$
  1. cymysql.connect is a context manager and so you should use it in a with statement.
  2. conn.cursor isn't a context manager. Which is very fishy, even more so that the original version of cymysql, pymysql, is.
  3. Seperate SQL interactions from UI interactions. This is as multiple parts of the UI may need to use the same SQL interactions, however since they're mangled, it'll lead to code duplication or errors in your UI.
  4. You don't need to do email = '', if you want to tell people it's a string then you can do email: str. A better thing to do however is use typing and make your code fully typed.
  5. You may want to verify that the email is a valid email address. It doesn't look like your SQL does that, but I don't know enough about it.
import cymysql
from getpass import getpass
def get_user_info():
 while True:
 email = input("Input your Email address (max. 64 chars.): ")
 password = getpass("Input a password (max. 64 chars.): ")
 if len(email) < 64 and len(password) < 64:
 return email, password
def register(cur, email, password):
 cur.execute("INSERT INTO `users` (`Email`, `Password`) VALUES (%s, %s)", (email, password))
def login(cur, email, password):
 cur.execute("SELECT * FROM `users` WHERE `Email`=%s AND `Password`=%s LIMIT 1", (email, password))
 return bool(cur.fetchall())
def check_account(cur, email):
 cur.execute("SELECT * FROM `users` WHERE `Email`=%s LIMIT 1", (email,))
 return bool(cur.fetchone())
def main():
 conn = cymysql.connect(
 host='127.0.0.1',
 user='root',
 passwd='',
 db='david'
 )
 with conn:
 cur = conn.cursor()
 email, password = get_user_info()
 check = check_account(cur, email)
 if check:
 loggedin = login(cur, email, password)
 if loggedin:
 print("You've succesfully logged-in!")
 else:
 print("You failed logging-in!")
 else:
 register(cur, email, password)
 print("You've succesfully registered!")
 cur.close()
if __name__ == '__main__':
 main()
answered Mar 23, 2019 at 18:57
\$\endgroup\$
2
  • \$\begingroup\$ Hey! Thanks for the answer, but in my original script I have conn.close() at the end of main(). Should I use it in the updated script too? \$\endgroup\$ Commented Mar 23, 2019 at 19:10
  • \$\begingroup\$ @MariaLaura You're not guarantee to reach the conn.close() - If I raise a keyboard interrupt, or kill the script does conn.close() run your way? \$\endgroup\$ Commented Mar 23, 2019 at 19:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.