saved new info in database upload is correect
This commit is contained in:
@@ -19,7 +19,7 @@ try:
|
||||
CREATE TABLE IF NOT EXISTS scan1_orders (
|
||||
Id INT AUTO_INCREMENT PRIMARY KEY, -- Auto-incremented ID with 6 digits
|
||||
operator_code VARCHAR(4) NOT NULL, -- Operator code with 4 characters
|
||||
CP_full_code VARCHAR(15) NOT NULL, -- Full CP code with up to 15 characters
|
||||
CP_full_code VARCHAR(15) NOT NULL UNIQUE, -- Full CP code with up to 15 characters
|
||||
OC1_code VARCHAR(4) NOT NULL, -- OC1 code with 4 characters
|
||||
OC2_code VARCHAR(4) NOT NULL, -- OC2 code with 4 characters
|
||||
CP_base_code VARCHAR(10) GENERATED ALWAYS AS (LEFT(CP_full_code, 10)) STORED, -- Auto-generated base code (first 10 characters of CP_full_code)
|
||||
|
||||
@@ -14,46 +14,53 @@ try:
|
||||
cursor = conn.cursor()
|
||||
print("Connected to the database successfully!")
|
||||
|
||||
# Create trigger for approved_quantity
|
||||
# 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 = """
|
||||
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
|
||||
SELECT COUNT(*)
|
||||
FROM scan1_orders
|
||||
WHERE CP_base_code = NEW.CP_base_code
|
||||
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//
|
||||
DELIMITER ;
|
||||
END;
|
||||
"""
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user