correct update to the database and database created for scan

This commit is contained in:
2025-04-22 16:12:17 +03:00
parent e51e4bf2bb
commit 1d6eadf540
9 changed files with 246 additions and 36 deletions

View File

@@ -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)