0% found this document useful (0 votes)
29 views6 pages

SQLite Login with Python Tkinter Guide

Uploaded by

Menberu Munye
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views6 pages

SQLite Login with Python Tkinter Guide

Uploaded by

Menberu Munye
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Login with SQLite in Python tkinter | SQLite3 in

Python
Anil SinghaniaDecember 30, 2020

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.

Complete SQLite with Python


Insert data into SQLite using Python tkinter
Fetch and Display data from SQLite in Python tkinter
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=[Link]("[Link]")
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=[Link]("[Link]")
print("Database Opened successfully")
[Link]("""
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=[Link]("[Link]")
print("Database Opened successfully")

[Link]("INSERT INTO ADMIN(USERNAME,PASSWORD) VALUES ('admin',


'admin789')");

[Link]("INSERT INTO ADMIN(USERNAME,PASSWORD) VALUES ('krazy',


'krazy789')");

[Link]()
print ("Records inserted successfully")
[Link]()
"""
###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 = [Link]('[Link]')
#display recrod
cursor = [Link]("SELECT * from ADMIN")
print("ID\tUSERNAME\tPASSWORD")
for row in cursor:
print ("{}\t{}\t\t{}".format(row[0],row[1],row[2]))
[Link]()

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

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=[Link]()
pwd=[Link]()
#applying empty validation
if uname=='' or pwd=='':
[Link]("fill the empty field!!!")
else:
#open database
conn = [Link]('[Link]')
#select query
cursor = [Link]('SELECT * from ADMIN where USERNAME="%s" and
PASSWORD="%s"'%(uname,pwd))
#fetch data
if [Link]():
[Link]("Login success")
else:
[Link]("Wrong username or password!!!")
#defining loginform function
def Loginform():
global login_screen
login_screen = Tk()
#Setting title of screen
login_screen.title("[Link]")
#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 Success: When you enter username and password which exists in database
the you will get a message "Login Success"

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"
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.

You might also like