Simple login system program in python using mysql


Today I will be sharing a simple code for login system in python. This code consists of two option sign up and sign in. I have used mysql to save the user data (id and password). This code has no functionality to it , it just allows user to sign in and sign up. My purpose of sharing the code is to show you how you can build a login system and if you want you can apply it in any of your projects.

Prerequisite

1) Python IDLE (click here to download)

2) Wamp server (click here to download)
Note:- wamp server is a mysql server. You can use any server but I personally use wamp server.

3) Mysql connector
You can install it by typing in the command "pip install mysql-connector" in cmd, power shell or terminal etc according to your operating system

Now that you have everything installed it's time to look into code

Code


import mysql.connector as conn
db=conn.connect(user="root",passwd="")
cursor=db.cursor()
cursor.execute("create database if not exists user")
cursor.execute("use user")
cursor.execute("""CREATE TABLE if not exists login (
userid VARCHAR( 20 ) NOT NULL ,
password VARCHAR( 20 ) NOT NULL
)""")
while True:
    print("""Select your operation
1)signup
2)signin
3)exit""")
    ch=input("Enter your choice:")
    if ch=="1":
        user=input("Enter a userid:")
        password=input("Enter a password:")
        cursor.execute("insert into login values((%s),(%s))",(user,password))
        db.commit()
        print("\nYou have successfully registered\n")
    elif ch=="2":
        while True:
            uid=input("Enter a userid:")
            passwd=input("Enter a password:")
            user=[]
            password=[]
            cursor.execute("select userid from login")
            for i in cursor.fetchall():
                user.append(i[0])
            cursor.execute("select password from login")
            for i in cursor.fetchall():
                password.append(i[0])
            if uid in user:
                index=user.index(uid)
                if passwd==password[index]:
                    print("\nlogin successfull\n")
                    break
                else:
                    print("\nwrong password\n")
            else:
                print("\nuser doesn't exists\n")
    elif ch=="3":
        db.close()
        break


Output


Conclusion

I hope you have learnt something from this post if you have any queries related to code you can comment down and if you liked it don't forget to share and you can suggest me what I can do next.
Newest
Previous
Next Post »