From 1d6eadf540821b233244ddcac6ab1bf2b053b164 Mon Sep 17 00:00:00 2001 From: ske087 Date: Tue, 22 Apr 2025 16:12:17 +0300 Subject: [PATCH] correct update to the database and database created for scan --- py_app/app/__pycache__/routes.cpython-311.pyc | Bin 14563 -> 15785 bytes .../app/db_create_scripts/create_scan_1db.py | 42 ++++++++++++ .../app/db_create_scripts/create_triggers.py | 63 ++++++++++++++++++ py_app/app/query.py | 34 ++++++++++ py_app/app/routes.py | 60 ++++++++++++----- py_app/app/templates/scan.html | 28 ++++---- py_app/app/test.py | 37 ++++++++++ py_app/instance/external_server.conf | 8 +-- to_do.txt | 10 ++- 9 files changed, 246 insertions(+), 36 deletions(-) create mode 100644 py_app/app/db_create_scripts/create_scan_1db.py create mode 100644 py_app/app/db_create_scripts/create_triggers.py create mode 100644 py_app/app/query.py create mode 100644 py_app/app/test.py diff --git a/py_app/app/__pycache__/routes.cpython-311.pyc b/py_app/app/__pycache__/routes.cpython-311.pyc index ea201277e59480b725eec5b9b00f012b756b993a..b39d66d2f84bfd6fe90b1f0dd12617e16314525e 100644 GIT binary patch delta 3407 zcmb_eeQXow8Nc)S<80sAahy0Yj>(ZYaZElMn+DVnN<$JLAs=l>*=VPwPVOKE#}2PA z1cHN6wMl_S(;g1S+caqRV1X9KeknsqY(}DpY3_? z90yaXY0_T5_j{kOd!Fb0xYy5LPu)3Z`x(z$C}{hBY?{i{ePGKlv^jmBIZxwX8>)8G zqRCCPYD>~0Tf&()IeC+$z|P{P@NaA`ACT5f&3oK!14Z41M{9?v06#yB|85AGhQcTk z?%jty#Fc4(cWIOo#d6~6Myi7dZu#o=Dwo^>=3KAz

Y#m*wk-{+X;}d){i~jB6CYLjrlqXfUSVH2;OJZHFkxNr0pt&~4JF z8U*+a>v`IVEu62ZXrw9<3}^*GK&>$54AJ=UNDOrXU7J3_y+G5|_&UF*QdOG8aAImA z9!4VC3kI#}zwzBn$^#^-A>buIP6FCWpq9Wk0(AuH3FrX=Ce%adUIKms0Rr0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g-A=t8C%w_#PX~K!M;yC% z2u3--52a@DM|U>Z$w7R>Z@PnXwx%bYVwXcg|uco|*i&Y@*Ik5(w_a@ffNKClFH;IIUr`18iRahs5uHZMv{CP z_UFzdt%AOqT?D*(EuB-d>J7hmHhO9QO@CgZ2#Iz7KE16bCzX}|4FM`sOT zb9e{nFMVBf)7KW|fZX2Ru9;9v>+M8M#~Ti9I>Zgf{<35L9et(6eXr+k&pY0Cy?2Zi zll^V`TlP}N#D;0IY?>^YCM#{-bfkBWvkmMT4m}0>M(^MZ2Nan8+K8+@>yd~4k}$F% zjFg3ul72*Ch3U5kqlYK@&}V&?Nh|l+A=f0&ea;(T{JAT-_e2;y3R@DL+@ld!qLur7 zt0B>O_rS+ctv)#ra%t7d2&F84O(hq>*6YP@4!p?Onw8IwNe3PEdyI94ru-S(cR|mu zZ`4ith2P`R%m^7GL%yY05hXI{`~bS*rmqZ-7f%k%agIwcukuWs%1We@{wjDrIR~6p zh{?Cu$i|>ye}1<}E5WQPvV5r5V6!Y`)w;hd2<}5yzG0Q>c=3M7!3oSE(ac<-pAMaz z)SNLFRih*?$f{g-rK{^`4%o)rF)!4W>JAn_@CkI~&zb{zKV&W53ln{}=A;!Q#Lb|c zL1VFeM?QS@e--_zX)*mhh@U7`9*`Zt+7VDSER(D%L+O8}O=i7$66`MhbZofYpn}35 zisy#^jg6XTAH8sNZjGJ9RRe*KENds0b}H4+33dW#b-up0F0v{6c!aGeVQ)>CO)|zH z!hn73C#I3bymW0r9wRql%KpYE--orUaTwzYGT+v&3bNg1m(Et9XChvDIx^>Q<6sRn gAw)llJnrnrqcsHZC5Lo@G9Iky7&D9MuSfp*ALDvb$^ZZW diff --git a/py_app/app/db_create_scripts/create_scan_1db.py b/py_app/app/db_create_scripts/create_scan_1db.py new file mode 100644 index 0000000..c2b6bcf --- /dev/null +++ b/py_app/app/db_create_scripts/create_scan_1db.py @@ -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}") \ No newline at end of file diff --git a/py_app/app/db_create_scripts/create_triggers.py b/py_app/app/db_create_scripts/create_triggers.py new file mode 100644 index 0000000..f0bf158 --- /dev/null +++ b/py_app/app/db_create_scripts/create_triggers.py @@ -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}") \ No newline at end of file diff --git a/py_app/app/query.py b/py_app/app/query.py new file mode 100644 index 0000000..57e72bb --- /dev/null +++ b/py_app/app/query.py @@ -0,0 +1,34 @@ +import mariadb + +# Database connection credentials +db_config = { + "user": "trasabilitate", + "password": "Initial01!", + "host": "localhost", + "database": "trasabilitate_database" +} + +try: + # Connect to the database + conn = mariadb.connect(**db_config) + cursor = conn.cursor() + + # Query to fetch all records from the scan1 table + query = "SELECT * FROM scan1_orders ORDER BY Id DESC LIMIT 15" + cursor.execute(query) + + # Fetch and print the results + rows = cursor.fetchall() + if rows: + print("Records in the 'scan1_orders' table:") + for row in rows: + print(row) + else: + print("No records found in the 'scan1_orders' table.") + + # Close the connection + cursor.close() + conn.close() + +except mariadb.Error as e: + print(f"Error connecting to the database: {e}") \ No newline at end of file diff --git a/py_app/app/routes.py b/py_app/app/routes.py index 9d96574..598019d 100644 --- a/py_app/app/routes.py +++ b/py_app/app/routes.py @@ -1,5 +1,5 @@ import os -import pyodbc +import mariadb from flask import Blueprint, render_template, redirect, url_for, request, flash, session, current_app from .models import User from . import db @@ -7,7 +7,7 @@ from . import db bp = Blueprint('main', __name__) def get_db_connection(): - """Reads the external_server.conf file and returns a database connection.""" + """Reads the external_server.conf file and returns a MariaDB database connection.""" settings_file = os.path.join(current_app.instance_path, 'external_server.conf') if not os.path.exists(settings_file): raise FileNotFoundError("The external_server.conf file is missing in the instance folder.") @@ -19,15 +19,14 @@ def get_db_connection(): key, value = line.strip().split('=', 1) settings[key] = value - # Create a database connection string - connection_string = ( - f"DRIVER={{ODBC Driver 17 for SQL Server}};" - f"SERVER={settings['server_domain']},{settings['port']};" - f"DATABASE={settings['database_name']};" - f"UID={settings['username']};" - f"PWD={settings['password']};" + # Create a database connection + return mariadb.connect( + user=settings['username'], + password=settings['password'], + host=settings['server_domain'], + port=int(settings['port']), + database=settings['database_name'] ) - return pyodbc.connect(connection_string) @bp.route('/login', methods=['GET', 'POST']) def login(): @@ -99,18 +98,37 @@ def scan(): date = request.form.get('date') time = request.form.get('time') + # Print the values to the terminal for debugging with single quotes + print("Values to be inserted:") + print(f"Operator Code: '{operator_code}'") + print(f"CP Code: '{cp_code}'") + print(f"OC1 Code: '{oc1_code}'") + print(f"OC2 Code: '{oc2_code}'") + print(f"Defect Code: '{defect_code}'") + print(f"Date: '{date}'") + print(f"Time: '{time}'") + try: + # Connect to the database conn = get_db_connection() cursor = conn.cursor() - cursor.execute( - "INSERT INTO scanare (operator_code, cp_code, oc1_code, oc2_code, defect_code, date, time, quantity) " - "VALUES (?, ?, ?, ?, ?, ?, ?, ?)", - operator_code, cp_code, oc1_code, oc2_code, defect_code, date, time, 1 - ) + + # Insert query + insert_query = """ + INSERT INTO scan1_orders (operator_code, CP_full_code, OC1_code, OC2_code, quality_code, date, time) + VALUES (?, ?, ?, ?, ?, ?, ?) + """ + print(f"Executing query: {insert_query}") + print(f"With values: ('{operator_code}', '{cp_code}', '{oc1_code}', '{oc2_code}', '{defect_code}', '{date}', '{time}')") + + # Execute the query + cursor.execute(insert_query, (operator_code, cp_code, oc1_code, oc2_code, defect_code, date, time)) conn.commit() conn.close() + flash('Scan data saved successfully.') - except Exception as e: + except mariadb.Error as e: + print(f"Error saving scan data: {e}") flash(f"Error saving scan data: {e}") # Fetch the latest scan data for display @@ -118,10 +136,16 @@ def scan(): try: conn = get_db_connection() cursor = conn.cursor() - cursor.execute("SELECT TOP 14 * FROM scanare ORDER BY id DESC") + cursor.execute(""" + SELECT Id, operator_code, CP_full_code, OC1_code, OC2_code, quality_code, date, time, approved_quantity, rejected_quantity + FROM scan1_orders + ORDER BY Id DESC + LIMIT 15 + """) scan_data = cursor.fetchall() conn.close() - except Exception as e: + except mariadb.Error as e: + print(f"Error fetching scan data: {e}") flash(f"Error fetching scan data: {e}") return render_template('scan.html', scan_data=scan_data) diff --git a/py_app/app/templates/scan.html b/py_app/app/templates/scan.html index 42e5e81..c39af97 100644 --- a/py_app/app/templates/scan.html +++ b/py_app/app/templates/scan.html @@ -10,7 +10,7 @@ - + @@ -22,7 +22,7 @@ - + @@ -38,28 +38,30 @@ ID - Operator Code + Op Code CP Code OC1 Code OC2 Code Defect Code Date Time - Quantity + Apr. Quantity + Rejec. Quantity {% for row in scan_data %} - {{ row.id }} - {{ row.operator_code }} - {{ row.cp_code }} - {{ row.oc1_code }} - {{ row.oc2_code }} - {{ row.defect_code }} - {{ row.date }} - {{ row.time }} - {{ row.quantity }} + {{ row[0] }} + {{ row[1] }} + {{ row[2] }} + {{ row[3] }} + {{ row[4] }} + {{ row[5] }} + {{ row[6] }} + {{ row[7] }} + {{ row[8] }} + {{ row[9] }} {% endfor %} diff --git a/py_app/app/test.py b/py_app/app/test.py new file mode 100644 index 0000000..76544e8 --- /dev/null +++ b/py_app/app/test.py @@ -0,0 +1,37 @@ +import mariadb + +# Database connection credentials +def get_db_connection(): + return mariadb.connect( + user="trasabilitate", # Replace with your username + password="Initial01!", # Replace with your password + host="localhost", # Replace with your host + port=3306, # Default MariaDB port + database="trasabilitate_database" # Replace with your database name + ) + +try: + # Connect to the database + conn = get_db_connection() + cursor = conn.cursor() + + # Insert query + insert_query = """ + INSERT INTO scan1_orders (operator_code, CP_full_code, OC1_code, OC2_code, quality_code, date, time) + VALUES (?, ?, ?, ?, ?, ?, ?) + """ + # Values to insert + values = ('OP01', 'CP12345678-0002', 'OC11', 'OC22', 000, '2025-04-22', '14:30:00') + + # Execute the query + cursor.execute(insert_query, values) + conn.commit() + + print("Test data inserted successfully into scan1_orders.") + + # Close the connection + cursor.close() + conn.close() + +except mariadb.Error as e: + print(f"Error inserting data: {e}") \ No newline at end of file diff --git a/py_app/instance/external_server.conf b/py_app/instance/external_server.conf index ea3c87d..271cc7b 100644 --- a/py_app/instance/external_server.conf +++ b/py_app/instance/external_server.conf @@ -1,5 +1,5 @@ -server_domain=testserver.com +server_domain=localhost port=3602 -database_name=recticel -username=test1 -password=12345678 +database_name=trasabilitate_database +username=trasabilitate +password=Initial01! diff --git a/to_do.txt b/to_do.txt index 997a0c4..c692dda 100644 --- a/to_do.txt +++ b/to_do.txt @@ -2,4 +2,12 @@ python3 -m venv recticel source recticel/bin/activate python /home/ske087/quality_recticel/py_app/run.py - \ No newline at end of file + sudo mysql -u root -p + + root password : Initaial01! + + CREATE DATABASE trasabilitate_database; + CREATE USER 'trasabilitate'@'localhost' IDENTIFIED BY 'Initial01!'; + GRANT ALL PRIVILEGES ON trasabilitate_database.* TO 'trasabilitate'@'localhost'; + FLUSH PRIVILEGES; + EXIT \ No newline at end of file