70 lines
2.3 KiB
Python
Executable File
70 lines
2.3 KiB
Python
Executable File
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!")
|
|
|
|
# Delete old triggers if they exist
|
|
try:
|
|
cursor.execute("DROP TRIGGER IF EXISTS increment_approved_quantity;")
|
|
print("Old trigger 'increment_approved_quantity' deleted successfully.")
|
|
except mariadb.Error as e:
|
|
print(f"Error deleting old trigger 'increment_approved_quantity': {e}")
|
|
|
|
try:
|
|
cursor.execute("DROP TRIGGER IF EXISTS increment_rejected_quantity;")
|
|
print("Old trigger 'increment_rejected_quantity' deleted successfully.")
|
|
except mariadb.Error as e:
|
|
print(f"Error deleting old trigger 'increment_rejected_quantity': {e}")
|
|
|
|
# Create corrected trigger for approved_quantity
|
|
create_approved_trigger = """
|
|
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 COUNT(*)
|
|
FROM scan1_orders
|
|
WHERE CP_base_code = NEW.CP_base_code AND quality_code = 000
|
|
) + 1;
|
|
SET NEW.rejected_quantity = (
|
|
SELECT COUNT(*)
|
|
FROM scan1_orders
|
|
WHERE CP_base_code = NEW.CP_base_code AND quality_code != 000
|
|
);
|
|
ELSE
|
|
SET NEW.approved_quantity = (
|
|
SELECT COUNT(*)
|
|
FROM scan1_orders
|
|
WHERE CP_base_code = NEW.CP_base_code AND quality_code = 000
|
|
);
|
|
SET NEW.rejected_quantity = (
|
|
SELECT COUNT(*)
|
|
FROM scan1_orders
|
|
WHERE CP_base_code = NEW.CP_base_code AND quality_code != 000
|
|
) + 1;
|
|
END IF;
|
|
END;
|
|
"""
|
|
cursor.execute(create_approved_trigger)
|
|
print("Trigger 'increment_approved_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}") |