#!/usr/bin/env python3 """ CSV Import Script for MariaDB Database This script imports data from sept.csv into the offsystemsCounting table """ import csv from database_manager import DatabaseManager def import_csv_data(csv_file_path): """Import data from CSV file into the database.""" print("CSV Import Script for MariaDB Database") print("=" * 40) # Initialize database connection db = DatabaseManager() # Check if we can connect conn = db.get_connection() if not conn or not conn.is_connected(): print("āŒ Cannot connect to MariaDB database.") print("Please make sure:") print("1. MariaDB server is running") print("2. Database 'cantare_injectie' exists") print("3. User 'omron' has proper permissions") return False print("āœ… Successfully connected to MariaDB database") # Read and import CSV data try: with open(csv_file_path, 'r', newline='', encoding='utf-8') as csvfile: reader = csv.DictReader(csvfile) # Check if the required columns exist if 'id' not in reader.fieldnames or 'mass' not in reader.fieldnames: print("āŒ CSV file must contain 'id' and 'mass' columns") return False print(f"šŸ“ Reading data from: {csv_file_path}") print(f"šŸ“‹ Columns found: {reader.fieldnames}") # Import data imported_count = 0 updated_count = 0 error_count = 0 for row_num, row in enumerate(reader, start=2): # Start at 2 because line 1 is header try: record_id = row['id'].strip() mass_str = row['mass'].strip() # Validate data if not record_id: print(f"āš ļø Row {row_num}: Empty ID, skipping") error_count += 1 continue if len(record_id) > 20: print(f"āš ļø Row {row_num}: ID '{record_id}' too long (max 20 chars), skipping") error_count += 1 continue try: mass = float(mass_str) except ValueError: print(f"āš ļø Row {row_num}: Invalid mass value '{mass_str}', skipping") error_count += 1 continue # Check if record exists existing = db.search_by_id(record_id) # Add or update record success = db.add_or_update_record(record_id, mass) if success: if existing: updated_count += 1 if updated_count <= 5: # Show first 5 updates print(f"šŸ”„ Updated: {record_id} = {mass}") else: imported_count += 1 if imported_count <= 5: # Show first 5 imports print(f"āž• Added: {record_id} = {mass}") else: error_count += 1 print(f"āŒ Failed to import row {row_num}: {record_id}") # Progress indicator if (imported_count + updated_count + error_count) % 100 == 0: print(f"šŸ“Š Progress: {imported_count + updated_count + error_count} records processed...") except Exception as e: error_count += 1 print(f"āŒ Error processing row {row_num}: {e}") # Final summary print("\n" + "=" * 40) print("šŸ“Š IMPORT SUMMARY") print("=" * 40) print(f"āœ… New records added: {imported_count}") print(f"šŸ”„ Records updated: {updated_count}") print(f"āŒ Errors: {error_count}") print(f"šŸ“ˆ Total processed: {imported_count + updated_count + error_count}") # Verify final count in database total_records = db.get_record_count() print(f"šŸ“‹ Total records in database: {total_records}") if imported_count > 0 or updated_count > 0: print("\nāœ… CSV import completed successfully!") return True else: print("\nāš ļø No records were imported.") return False except FileNotFoundError: print(f"āŒ CSV file not found: {csv_file_path}") return False except Exception as e: print(f"āŒ Error reading CSV file: {e}") return False finally: # Close database connection db.close_connection() if __name__ == "__main__": csv_file = "sept.csv" success = import_csv_data(csv_file) if success: print("\nšŸš€ You can now run the Kivy app to view the imported data:") print(" python main.py") print(" or") print(" ./run_app.sh") else: print("\nāŒ Import failed. Please check the errors above.")