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 (
|
CREATE TABLE IF NOT EXISTS scan1_orders (
|
||||||
Id INT AUTO_INCREMENT PRIMARY KEY, -- Auto-incremented ID with 6 digits
|
Id INT AUTO_INCREMENT PRIMARY KEY, -- Auto-incremented ID with 6 digits
|
||||||
operator_code VARCHAR(4) NOT NULL, -- Operator code with 4 characters
|
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
|
OC1_code VARCHAR(4) NOT NULL, -- OC1 code with 4 characters
|
||||||
OC2_code VARCHAR(4) NOT NULL, -- OC2 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)
|
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()
|
cursor = conn.cursor()
|
||||||
print("Connected to the database successfully!")
|
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 = """
|
create_approved_trigger = """
|
||||||
DELIMITER //
|
|
||||||
CREATE TRIGGER increment_approved_quantity
|
CREATE TRIGGER increment_approved_quantity
|
||||||
BEFORE INSERT ON scan1_orders
|
BEFORE INSERT ON scan1_orders
|
||||||
FOR EACH ROW
|
FOR EACH ROW
|
||||||
BEGIN
|
BEGIN
|
||||||
IF NEW.quality_code = 000 THEN
|
IF NEW.quality_code = 000 THEN
|
||||||
SET NEW.approved_quantity = (
|
SET NEW.approved_quantity = (
|
||||||
SELECT IFNULL(MAX(approved_quantity), 0) + 1
|
SELECT COUNT(*)
|
||||||
FROM scan1_orders
|
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 IF;
|
||||||
END//
|
END;
|
||||||
DELIMITER ;
|
|
||||||
"""
|
"""
|
||||||
cursor.execute(create_approved_trigger)
|
cursor.execute(create_approved_trigger)
|
||||||
print("Trigger 'increment_approved_quantity' created successfully!")
|
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
|
# Commit changes and close the connection
|
||||||
conn.commit()
|
conn.commit()
|
||||||
cursor.close()
|
cursor.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user