correct update to the database and database created for scan
This commit is contained in:
42
py_app/app/db_create_scripts/create_scan_1db.py
Normal file
42
py_app/app/db_create_scripts/create_scan_1db.py
Normal file
@@ -0,0 +1,42 @@
|
||||
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 the scan1_orders table
|
||||
create_table_query = """
|
||||
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
|
||||
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)
|
||||
quality_code INT(3) NOT NULL, -- Quality code with 3 digits
|
||||
date DATE NOT NULL, -- Date in format dd-mm-yyyy
|
||||
time TIME NOT NULL, -- Time in format hh:mm:ss
|
||||
approved_quantity INT DEFAULT 0, -- Auto-incremented quantity for quality_code = 000
|
||||
rejected_quantity INT DEFAULT 0 -- Auto-incremented quantity for quality_code != 000
|
||||
);
|
||||
"""
|
||||
cursor.execute(create_table_query)
|
||||
print("Table 'scan1_orders' 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: {e}")
|
||||
63
py_app/app/db_create_scripts/create_triggers.py
Normal file
63
py_app/app/db_create_scripts/create_triggers.py
Normal 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}")
|
||||
Reference in New Issue
Block a user