-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
51 lines (50 loc) · 2.1 KB
/
database.py
File metadata and controls
51 lines (50 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import pymysql
from tkinter import messagebox
def connectdatabase(password):
global mycursor,conn
try:
conn = pymysql.connect(host='localhost',user='root',password=password)
mycursor = conn.cursor()
except:
messagebox.showerror("Error","Something Went wrong.Please open mysql database before running again")
return
mycursor.execute('CREATE DATABASE IF NOT EXISTS employee_data')
mycursor.execute('USE employee_data')
mycursor.execute('CREATE TABLE IF NOT EXISTS data (Id varchar(20),Name varchar(50),Phone varchar(15),Role varchar(50),Gender varchar(10),Salary decimal(10,2))')
return True
def insert(id,name,phone,role,gender,salary):
mycursor.execute('INSERT INTO data VALUES (%s,%s,%s,%s,%s,%s)',(id,name,phone,role,gender,salary))
conn.commit()
def id_exists(id):
mycursor.execute('SELECT COUNT(*) FROM data WHERE id=%s',id)
result = mycursor.fetchone()
return result[0]>0
def fetch_employees():
mycursor.execute('SELECT * FROM data')
result = mycursor.fetchall()
return result
def search(searchby,data):
if searchby=="Id":
mycursor.execute('SELECT * FROM data WHERE Id=%s',(data,))
elif searchby=="Name":
mycursor.execute('SELECT * FROM data WHERE Name=%s',(data,))
elif searchby=="Phone":
mycursor.execute('SELECT * FROM data WHERE Phone=%s',(data,))
elif searchby=="Role":
mycursor.execute('SELECT * FROM data WHERE Role=%s',(data,))
elif searchby=="Gender":
mycursor.execute('SELECT * FROM data WHERE Gender=%s',(data,))
elif searchby=="Salary":
mycursor.execute('SELECT * FROM data WHERE Salary=%s',(data,))
result = mycursor.fetchall()
return result
def updatedata(id,name,phone,role,gender,salary):
mycursor.execute("UPDATE data SET Name = %s,Phone = %s,Role = %s,Gender = %s,Salary = %s WHERE Id = %s", (name, phone, role, gender, salary, id))
conn.commit()
def delete(id):
mycursor.execute('DELETE FROM data WHERE Id=%s',id)
conn.commit()
def deleteall():
mycursor.execute('Truncate table data')
mycursor.fetchall()
conn.commit()