Add logging functionality and database interaction in db.py and main.py

This commit is contained in:
Mabe
2026-08-25 20:10:40 +02:00
commit e60507eac2
3 changed files with 261 additions and 0 deletions
View File
+100
View File
@@ -0,0 +1,100 @@
import mariadb
import os
import logging
#Logging
logging.basicConfig(
filename='app.log', # Log to a file
level=logging.DEBUG, # Log everything (DEBUG and above)
format='%(asctime)s %(levelname)s %(message)s'
)
logging.info('Application started')
try:
logging.debug('Debug info here')
except Exception as e:
logging.exception('An error occurred')
def connect():
try:
global connection
connection = mariadb.connect(user="luggage",
password="13579,QsC",
host="192.168.1.74",
port=3307,
database="canada_luggage")
print("Connected to MariaDB successfully!")
logging.info("Connected to MariaDB successfully!")
except mariadb.Error as e:
print(f"Error connecting to DB: {e}")
logging.error("Error connecting")
logging.debug(f"Error connecting to DB: {e}")
def new_part(cloth_type, weigth, pieces):
if type(pieces) is not int:
print("only ints allowed")
logging.error("only ints allowed")
else:
cursor = connection.cursor()
executed = "INSERT INTO luggage (clothes, weigth, pieces) VALUES (?, ?)"
executed_data = (cloth_type, weigth, pieces)
cursor.execute(executed, executed_data)
connection.commit()
logging.info("new Insert is in DB")
def fetch_clothes():
cursor = connection.cursor()
cursor.execute("SELECT clothes FROM luggage")
clothes = [str(row[0]) for row in cursor.fetchall()]
logging.info("fetched clothes")
return clothes
def update_weigth(cloth_type, weigth):
cursor = connection.cursor()
executed = "UPDATE luggage SET weigth = ? WHERE clothes = ?"
executed_data = (weigth, cloth_type)
cursor.execute(executed, executed_data)
connection.commit()
logging.info("weigth updated")
def update_pieces(cloth_type, pieces):
if type(pieces) is not int:
logging.error("only ints allowed")
else:
cursor = connection.cursor()
executed = "UPDATE luggage SET pieces = ? WHERE clothes = ?"
executed_data = (pieces, cloth_type)
cursor.execute(executed, executed_data)
connection.commit()
logging.info("pieces updated")
def fetch_weigth(cloth_type):
cursor = connection.cursor()
cursor.execute("SELECT weigth FROM luggage WHERE clothes = ?", (cloth_type,))
weigth = cursor.fetchone()[0]
logging.info("fetched weigth")
return weigth
def fetch_luggage_weigth():
cursor = connection.cursor()
cursor.execute("SELECT sum(weigth) FROM luggage")
weigth = cursor.fetchone()[0]
logging.info("fetched luggage weigth")
return weigth
def fetch_piece(cloth_type):
cursor = connection.cursor()
cursor.execute("SELECT pieces FROM luggage WHERE clothes = ?", (cloth_type,))
pieces = cursor.fetchone()[0]
logging.info("fetched luggage pieces")
return pieces
def fetch_all_pieces():
cursor = connection.cursor()
cursor.execute("SELECT sum(pieces) FROM luggage")
pieces = cursor.fetchone()[0]
+161
View File
@@ -0,0 +1,161 @@
import db
import logging
import time
import os
import sys
print("loading")
open("app.log", "w").close()
db.connect()
#Logging
logging.basicConfig(
filename='app.log', # Log to a file
level=logging.DEBUG, # Log everything (DEBUG and above)
format='%(asctime)s %(levelname)s %(message)s'
)
logging.info('Application started')
try:
logging.debug('Debug info here')
except Exception as e:
logging.exception('An error occurred')
def insert():
clothe = input("How is the clothe called? ").upper()
weigth = input("The weigth please? ")
pieces = input("How many pieces? ints only ")
db.new_part(clothe, weigth, pieces)
print("it is inserted")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def update():
clothe = input("How is the clothe called? ").upper()
weigth = input("The weigth please? ")
pieces = input("How many pieces? ints only ")
db.update_weigth(clothe, weigth)
db.update_pieces(clothe, pieces)
print("it is updated")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def get_weigth():
clothe = input("How is the clothe called? ").upper()
weigth = db.fetch_weigth(clothe)
print(weigth)
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def get_luggage_weigth():
weigth = db.fetch_luggage_weigth()
print(f"The luggage has a weigth of {weigth}kg")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def get_pieces():
clothe = input("How is the clothe called? ").upper()
pieces = db.fetch_piece(clothe)
print(f"You have {pieces} of {clothe}")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def get_all_pieces():
pieces = db.fetch_all_pieces()
print(f"You have {pieces} in your luggage")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def get_clothes():
clothes = db.fetch_clothes()
for clothe in clothes:
print(clothe)
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def get_weigth_pieces():
clothe = input("How is the clothe called? ").upper()
weigth = db.fetch_weigth(clothe)
pieces = db.fetch_piece(clothe)
print(f"You have {pieces} of {clothe}. The weigth is {weigth}")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def exit_programm():
time.sleep(0.5)
print(r"""
## ## ##########
### ### ## ##
#### #### ## ##
## #### ## ##########
## ## ## ## ##
## ## ## ##
""")
time.sleep(0.5)
print("")
print("")
print("Disconnect DB")
print("logout")
print("shutting down")
time.sleep(0.75)
os.system('cls' if os.name == 'nt' else 'clear')
sys.exit()
def menu():
print("Menu")
print("1: Insert Item")
print("2: Update Item")
print("3: Get weigth of clothe")
print("4: Get weigth of luggage")
print("5: Get pieces of clothe")
print("6: Get pieces of luggage")
print("7: Get clothes")
print("8: Get weigth and pieces of clothes")
print("0: EXIT")
choice = input("Type number here ")
os.system('cls' if os.name == 'nt' else 'clear')
logging.info(f"{choice} is choosen")
time.sleep(0.5)
if choice == "1":
insert()
if choice == "2":
update()
if choice == "3":
get_weigth()
if choice == "4":
get_luggage_weigth()
if choice == "5":
get_pieces()
if choice == "6":
get_all_pieces()
if choice == "7":
get_clothes()
if choice == "8":
get_weigth_pieces()
if choice == "0":
exit_programm()
#The Terminal App Starts here
time.sleep(5)
print(r"""
## ## ##########
### ### ## ##
#### #### ## ##
## #### ## ##########
## ## ## ## ##
## ## ## ##
""")
logging.info("Menu is starting")
time.sleep(2.5)
menu()