correct update to the database and database created for scan

This commit is contained in:
2025-04-22 16:12:17 +03:00
parent e51e4bf2bb
commit 1d6eadf540
9 changed files with 246 additions and 36 deletions

View File

@@ -0,0 +1,63 @@
import mariadb
# Database connection credentials
db_config = {
"user": "trasabilitate",
"password": "Initial01!",
"host": "localhost",
"database": "trasabilitate_database"
}
# Connect to the database
try:
conn = mariadb.connect(**db_config)
cursor = conn.cursor()
print("Connected to the database successfully!")
# Create trigger for approved_quantity
create_approved_trigger = """
DELIMITER //
CREATE TRIGGER increment_approved_quantity
BEFORE INSERT ON scan1_orders
FOR EACH ROW
BEGIN
IF NEW.quality_code = 000 THEN
SET NEW.approved_quantity = (
SELECT IFNULL(MAX(approved_quantity), 0) + 1
FROM scan1_orders
WHERE CP_base_code = NEW.CP_base_code
);
END IF;
END//
DELIMITER ;
"""
cursor.execute(create_approved_trigger)
print("Trigger 'increment_approved_quantity' created successfully!")
# Create trigger for rejected_quantity
create_rejected_trigger = """
DELIMITER //
CREATE TRIGGER increment_rejected_quantity
BEFORE INSERT ON scan1_orders
FOR EACH ROW
BEGIN
IF NEW.quality_code != 000 THEN
SET NEW.rejected_quantity = (
SELECT IFNULL(MAX(rejected_quantity), 0) + 1
FROM scan1_orders
WHERE CP_base_code = NEW.CP_base_code
);
END IF;
END//
DELIMITER ;
"""
cursor.execute(create_rejected_trigger)
print("Trigger 'increment_rejected_quantity' created successfully!")
# Commit changes and close the connection
conn.commit()
cursor.close()
conn.close()
except mariadb.Error as e:
print(f"Error connecting to the database or creating triggers: {e}")