Skip to content

Commit 556394e

Browse files
committed
Database queries added
1 parent 5c8da92 commit 556394e

File tree

1 file changed

+74
-3
lines changed

1 file changed

+74
-3
lines changed

Password-Manager-GUI/passwords.py

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,77 @@
11
from tkinter import *
2-
import sqlite3
32
from tkinter import messagebox
3+
import sqlite3
4+
from sqlite3 import Error
5+
6+
# Function to connect to the SQL Database
7+
def sql_connection():
8+
try:
9+
con = sqlite3.connect('passwordManager.db')
10+
return con
11+
except Error:
12+
print(Error)
13+
14+
# Function to create table
15+
def sql_table(con):
16+
cursorObj = con.cursor()
17+
cursorObj.execute("CREATE TABLE IF NOT EXISTS passwordStore(website text, username text, pass text,test text)")
18+
con.commit()
19+
20+
# Call functions to connect to database and create table
21+
con = sql_connection()
22+
sql_table(con)
23+
24+
#Create submit function for database
25+
def submit(con):
26+
cursor = con.cursor()
27+
#Insert Into Table
28+
if website.get()!="" and username.get()!="" and password.get()!="":
29+
cursor.execute("INSERT INTO passwordStore VALUES (:website, :username, :password)",
30+
{
31+
'website': website.get(),
32+
'username': username.get(),
33+
'password': password.get()
34+
}
35+
)
36+
con.commit()
37+
# Message box
38+
messagebox.showinfo("Info", "Record Added in Database!")
39+
40+
# After data entry clear the text boxes
41+
website.delete(0, END)
42+
username.delete(0, END)
43+
password.delete(0, END)
44+
45+
else:
46+
messagebox.showinfo("Alert", "Please fill all details!")
47+
48+
# Create Query Function
49+
def query(con):
50+
51+
#set button text
52+
query_btn.configure(text="Hide Records", command=hide)
53+
54+
cursor = con.cursor()
55+
56+
#Query the database
57+
cursor.execute("SELECT *, oid FROM passwordStore")
58+
records = cursor.fetchall()
59+
#print(records)
60+
61+
p_records = " Id \t\t Website \t\t Username \t\t Password\n"
62+
for record in records:
63+
p_records += str(record[3])+ "\t\t" +str(record[0])+ "\t\t" + str(record[1])+ "\t\t" + str(record[2]) + "\n"
64+
#print(record)
65+
66+
query_label['text'] = p_records
67+
# Commit changes
68+
con.commit()
69+
70+
#Create Function to Hide Records
71+
def hide():
72+
query_label['text'] = ""
73+
query_btn.configure(text="Show Records", command=lambda:query(con))
74+
475

576
root = Tk()
677
root.title("Password Manager")
@@ -29,9 +100,9 @@
29100

30101

31102
#Create Buttons
32-
submit_btn = Button(root, text = "Add Password")
103+
submit_btn = Button(root, text = "Add Password",command = lambda: submit(con))
33104
submit_btn.grid(row = 5, column=1, pady=5, padx=15, ipadx=35)
34-
query_btn = Button(root, text = "Show All")
105+
query_btn = Button(root, text = "Show All",command = lambda: query(con))
35106
query_btn.grid(row=6, column=1, pady=5, padx=5, ipadx=35)
36107

37108
#Create a Label to show stored passwords

0 commit comments

Comments
 (0)