0% found this document useful (0 votes)
5 views2 pages

Database Code

The document contains a Python script that connects to a MySQL database and manages employee data. It includes functions to create a database and table, insert, update, delete, and fetch employee records, as well as check for existing IDs and delete all records. The script utilizes the pymysql library for database operations and tkinter for error messaging.

Uploaded by

refixcare.in
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)
5 views2 pages

Database Code

The document contains a Python script that connects to a MySQL database and manages employee data. It includes functions to create a database and table, insert, update, delete, and fetch employee records, as well as check for existing IDs and delete all records. The script utilizes the pymysql library for database operations and tkinter for error messaging.

Uploaded by

refixcare.in
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
You are on page 1/ 2

import pymysql

from tkinter import messagebox

def connect_database():
global mycursor,conn
try:
conn = pymysql.connect(host='localhost', user='root',
password='332211')
mycursor = conn.cursor()
except:

messagebox.showerror('Error','Something went wrong,please open


mysql app 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(30),Name varchar(50),phone varchar(15),role varchar(50),Gender
varchar(20),Salary VARCHAR(10))')

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 update(id,new_name,new_Phone,new_role,new_Gender,new_salary):
mycursor.execute('UPDATE data SET name=%s,phone=%s,role=
%s,gender=%s,salary=%s WHERE id=%s',
(new_name,new_Phone,new_role,new_Gender,new_salary,id))
conn.commit()

def delete(id):
mycursor.execute('DELETE FROM data WHERE id=%s',id)
conn.commit()

def search(option,value):
mycursor.execute(f'SELECT * FROM data WHERE {option}=%s',value)
result=mycursor.fetchall()
return result

def deleteall_records():
mycursor.execute('TRUNCATE TABLE data')
conn.commit()

connect_database()

You might also like