Skip to content

Commit 7fdb602

Browse files
committed
fist commit
0 parents  commit 7fdb602

File tree

7 files changed

+97
-0
lines changed

7 files changed

+97
-0
lines changed

.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.env/
2+
3+
*.pyc
4+
__pycache__/
5+
6+
venv/
7+
env/
8+
9+
.vscode/
10+
.idea/
11+
*.swp

app/__init__.py

Whitespace-only changes.

app/_table.sql

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE employees (
2+
id INT AUTO_INCREMENT PRIMARY KEY,
3+
name VARCHAR(100) NOT NULL,
4+
position VARCHAR(100),
5+
salary DECIMAL(10, 2),
6+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
7+
);

app/dbConnect.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import mysql.connector
2+
from mysql.connector import Error
3+
import getpass
4+
5+
def dbConnect():
6+
try:
7+
password = getpass.getpass("Enter password to connect to the database: ")
8+
connection = mysql.connector.connect(
9+
user='root',
10+
password=password if password else '',
11+
host='localhost',
12+
database='python_load_excel_employee'
13+
)
14+
if connection.is_connected():
15+
print("Successfully connected to the database.")
16+
return connection
17+
except Error as err:
18+
print(f"Error connecting to the database: {err}")
19+
return None

app/functions.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from mysql.connector import Error
2+
3+
def create_employee(connection, name, position, salary):
4+
try:
5+
cursor = connection.cursor()
6+
query = "INSERT INTO employees (name, position, salary) VALUES (%s, %s, %s)"
7+
cursor.execute(query, (name, position, salary))
8+
connection.commit()
9+
print("Employee inserted successfully.")
10+
except Error as err:
11+
print(f"Failed to insert employee: {err}")
12+
13+
def read_employees(connection):
14+
try:
15+
cursor = connection.cursor()
16+
query = "SELECT * FROM employees"
17+
cursor.execute(query)
18+
for row in cursor.fetchall():
19+
print(row)
20+
except Error as err:
21+
print(f"Failed to read employees: {err}")
22+
23+
def update_employee_salary(connection, employee_id, new_salary):
24+
try:
25+
cursor = connection.cursor()
26+
query = "UPDATE employees SET salary = %s WHERE id = %s"
27+
cursor.execute(query, (new_salary, employee_id))
28+
connection.commit()
29+
print("Employee salary updated successfully.")
30+
except Error as err:
31+
print(f"Failed to update salary: {err}")
32+
33+
def delete_employee(connection, employee_id):
34+
try:
35+
cursor = connection.cursor()
36+
query = "DELETE FROM employees WHERE id = %s"
37+
cursor.execute(query, (employee_id,))
38+
connection.commit()
39+
print("Employee deleted successfully.")
40+
except Error as err:
41+
print(f"Failed to delete employee: {err}")

main.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from app.dbConnect import dbConnect
2+
from app.functions import create_employee, read_employees, update_employee_salary, delete_employee
3+
4+
if __name__ == "__main__":
5+
conn = dbConnect()
6+
if conn:
7+
# USAGE...
8+
# create_employee(conn, 'Alice', 'Manager', 55000)
9+
# read_employees(conn)
10+
# update_employee_salary(conn, 1, 60000)
11+
# delete_employee(conn, 1)
12+
conn.close()
13+
print("Connection closed.")
14+
15+

requirements.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mysql-connector-python==9.3.0
2+
mysqlclient==2.2.7
3+
numpy==2.2.5
4+
pandas==2.2.3

0 commit comments

Comments
 (0)