Files
db_interface/import_csv.py

138 lines
5.3 KiB
Python
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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.")