Login with SQLite in Python tkinter | SQLite3 in Python

Hello friends how are you, Today in this post "Login with SQLite in Python tkinter" i am going to teach you how you can login with  SQLite database using Python Tkinter GUI interface. If you are a Computer Science students and want to create GUI based application using Database then this post will help you definitely. if you don't know how to use SQLite in Python and How to insert data into python first visit the below link.

Now i am going to explain everything step by step

Step 1:Create Database
First we need to create a database in SQLite, i am going to create database with name student so following is the code to create SQLite database in Python. Just open any python file and type the code and run this code.

import  sqlite3
conn=sqlite3.connect("student.db")
print("Database created successfully")

In the first line i have imported the library sqlite3 which is needed to handle SQLite with Python.

Step 2:Create Table
Now i am going to create table with name ADMIN. Just type the following code into your python file and execute this code to create table inside your database.

import  sqlite3
conn=sqlite3.connect("student.db")
print("Database Opened successfully")
conn.execute("""
CREATE TABLE ADMIN(
ADMIN_ID INTEGER PRIMARY KEY AUTOINCREMENT  NOT NULL ,
USERNAME TEXT NOT NULL, 
PASSWORD TEXT NOT NULL)
""")
print ("Table ADMIN created successfully")
"""
###Output###
Database Opened successfully
Table ADMIN created successfully
"""

Here execute() is a predefined function which is used to execute any query of SQLite. If you want to see your table in SQLite then you can use DB Browser.

Step 3:Insert data into table

Now i am going to insert data into table. By using SQL query we can insert data into tables. Following is the code to insert data into table.

import  sqlite3
conn=sqlite3.connect("student.db")
print("Database Opened successfully")

conn.execute("INSERT INTO ADMIN(USERNAME,PASSWORD) VALUES ('admin', 'admin789')");

conn.execute("INSERT INTO ADMIN(USERNAME,PASSWORD) VALUES ('krazy', 'krazy789')");

conn.commit()
print ("Records inserted successfully")
conn.close()
"""
###Output###
Database Opened successfully
Records inserted successfully
"""

Step 4:Display data from table

Now its time to fetch these data from SQLite so just type the following code into your python file and execute this file.

#import library
import sqlite3
#open database
conn = sqlite3.connect('student.db')
#display recrod
cursor = conn.execute("SELECT * from ADMIN")
print("ID\tUSERNAME\tPASSWORD")
for row in cursor:
   print ("{}\t{}\t\t{}".format(row[0],row[1],row[2]))
conn.close()
 
You just type this code into your python file or you can copy this code for your personal use. When you will run this code you will get a screen like below

Login with SQLite in Python tkinter

Step 5:Login with SQLite & tkinter GUI

I have created a simple GUI interface for Login Form using very simple line of code. Following is the code to create a login form and perform login operation with SQLite database.

from tkinter import *
#import library
import sqlite3
#open databse

#defining login function
def login():
    #getting form data
    uname=username.get()
    pwd=password.get()
    #applying empty validation
    if uname=='' or pwd=='':
        message.set("fill the empty field!!!")
    else:
      #open database
      conn = sqlite3.connect('student.db')
      #select query
      cursor = conn.execute('SELECT * from ADMIN where USERNAME="%s" and PASSWORD="%s"'%(uname,pwd))
      #fetch data 
      if cursor.fetchone():
       message.set("Login success")
      else:
       message.set("Wrong username or password!!!")
#defining loginform function
def Loginform():
    global login_screen
    login_screen = Tk()
    #Setting title of screen
    login_screen.title("krazyprogrammers.com")
    #setting height and width of screen
    login_screen.geometry("350x250")
    login_screen["bg"]="#1C2833"
    #declaring variable
    global  message;
    global username
    global password
    username = StringVar()
    password = StringVar()
    message=StringVar()
    #Creating layout of login form
    Label(login_screen,width="300", text="Login From", bg="#0E6655",fg="white",font=("Arial",12,"bold")).pack()
    #Username Label
    Label(login_screen, text="Username * ",bg="#1C2833",fg="white",font=("Arial",12,"bold")).place(x=20,y=40)
    #Username textbox
    Entry(login_screen, textvariable=username,bg="#1C2833",fg="white",font=("Arial",12,"bold")).place(x=120,y=42)
    #Password Label
    Label(login_screen, text="Password * ",bg="#1C2833",fg="white",font=("Arial",12,"bold")).place(x=20,y=80)
    #Password textbox
    Entry(login_screen, textvariable=password ,show="*",bg="#1C2833",fg="white",font=("Arial",12,"bold")).place(x=120,y=82)
    #Label for displaying login status[success/failed]
    Label(login_screen, text="",textvariable=message,bg="#1C2833",fg="white",font=("Arial",12,"bold")).place(x=95,y=120)
    #Login button
    Button(login_screen, text="Login", width=10, height=1, command=login, bg="#0E6655",fg="white",font=("Arial",12,"bold")).place(x=125,y=170)
    login_screen.mainloop()
#calling function Loginform
Loginform()

Here in the above program i have explained each line of code with comment still you get it difficult to understand then let me know by comment.
Step 6:Run Code
Now copy this code into your python file and execute this file , You will get output screen like below.
Login with SQLite in Python tkinter

Login with Success: When you enter username and password which exists in database the you will get a message "Login Success"
Login with SQLite in Python tkinter

Here in the above form i have entered admin as username and admin789 as password and this record exists in our database so it is displaying Login Success.

Login with Failure: When you enter username and password which does not exist in database the you will get a message "Wrong username or password"

Login with SQLite in Python tkinter

Here in the above form i have entered wxyz as a username and no record exists with username wxyz so it is displaying wrong username or password.

I hope now you can Login with SQLite using Python Tkinter.


Request:-If you found this post helpful then let me know by your comment and share it with your friend. 

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thanks.😊

Post a Comment

1 Comments