192 lines
6.0 KiB
Python
192 lines
6.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
SQLite Datenkopier-Programm
|
|
Kopiert ausgewählte Spalten von einer SQLite-Datenbank in eine andere
|
|
"""
|
|
|
|
import sqlite3
|
|
import sys
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
|
|
# Zu kopierende Spalten
|
|
COLUMNS = [
|
|
'dateTime',
|
|
'barometer',
|
|
'outTemp',
|
|
'outHumidity',
|
|
'windSpeed',
|
|
'windDir',
|
|
'windGust',
|
|
'rainRate',
|
|
'rain'
|
|
]
|
|
|
|
|
|
def format_datetime(timestamp):
|
|
"""Konvertiert Unix-Timestamp in lesbares Format"""
|
|
try:
|
|
if timestamp is None:
|
|
return 'NULL'
|
|
return datetime.fromtimestamp(int(timestamp)).strftime('%Y-%m-%d %H:%M:%S')
|
|
except (ValueError, OSError):
|
|
return str(timestamp)
|
|
|
|
|
|
def get_table_name(cursor):
|
|
"""Ermittelt den Namen der Quelltabelle"""
|
|
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'")
|
|
tables = cursor.fetchall()
|
|
|
|
if not tables:
|
|
print("Fehler: Keine Tabellen in der Quelldatenbank gefunden!")
|
|
sys.exit(1)
|
|
|
|
if len(tables) == 1:
|
|
return tables[0][0]
|
|
|
|
print("\nVerfügbare Tabellen:")
|
|
for i, (table,) in enumerate(tables, 1):
|
|
print(f"{i}. {table}")
|
|
|
|
while True:
|
|
try:
|
|
choice = int(input("\nWählen Sie die Nummer der Quelltabelle: "))
|
|
if 1 <= choice <= len(tables):
|
|
return tables[choice - 1][0]
|
|
except (ValueError, IndexError):
|
|
pass
|
|
print("Ungültige Eingabe. Bitte erneut versuchen.")
|
|
|
|
|
|
def check_columns(cursor, table_name, columns):
|
|
"""Prüft, welche Spalten in der Tabelle existieren"""
|
|
cursor.execute(f"PRAGMA table_info({table_name})")
|
|
existing_cols = {row[1] for row in cursor.fetchall()}
|
|
|
|
available = [col for col in columns if col in existing_cols]
|
|
missing = [col for col in columns if col not in existing_cols]
|
|
|
|
return available, missing
|
|
|
|
|
|
def copy_data(source_db, target_db, source_table, target_table, columns):
|
|
"""Kopiert die Daten von der Quell- zur Zieldatenbank"""
|
|
|
|
# Verbindungen herstellen
|
|
conn_source = sqlite3.connect(source_db)
|
|
conn_target = sqlite3.connect(target_db)
|
|
|
|
try:
|
|
cursor_source = conn_source.cursor()
|
|
cursor_target = conn_target.cursor()
|
|
|
|
# Quelltabelle ermitteln
|
|
if source_table is None:
|
|
source_table = get_table_name(cursor_source)
|
|
|
|
print(f"\nQuelltabelle: {source_table}")
|
|
|
|
# Verfügbare Spalten prüfen
|
|
available_cols, missing_cols = check_columns(cursor_source, source_table, columns)
|
|
|
|
if missing_cols:
|
|
print(f"\nWarnung: Folgende Spalten existieren nicht in der Quelltabelle: {', '.join(missing_cols)}")
|
|
|
|
if not available_cols:
|
|
print("Fehler: Keine der gewünschten Spalten gefunden!")
|
|
return False
|
|
|
|
print(f"Zu kopierende Spalten: {', '.join(available_cols)}")
|
|
|
|
# Zieltabelle erstellen
|
|
columns_def = ', '.join([f"{col} REAL" if col != 'dateTime' else f"{col} INTEGER PRIMARY KEY"
|
|
for col in available_cols])
|
|
cursor_target.execute(f"CREATE TABLE IF NOT EXISTS {target_table} ({columns_def})")
|
|
|
|
# Daten zählen
|
|
cursor_source.execute(f"SELECT COUNT(*) FROM {source_table}")
|
|
total_rows = cursor_source.fetchone()[0]
|
|
print(f"\nGesamtanzahl Datensätze: {total_rows}")
|
|
|
|
# Vorschau der ersten Zeile
|
|
cursor_source.execute(f"SELECT {columns_str} FROM {source_table} LIMIT 1")
|
|
first_row = cursor_source.fetchone()
|
|
if first_row:
|
|
print("\nVorschau erste Zeile:")
|
|
for i, col in enumerate(available_cols):
|
|
value = first_row[i]
|
|
if col == 'dateTime':
|
|
print(f" {col}: {format_datetime(value)} (Timestamp: {value})")
|
|
else:
|
|
print(f" {col}: {value}")
|
|
|
|
# Daten kopieren
|
|
columns_str = ', '.join(available_cols)
|
|
placeholders = ', '.join(['?' for _ in available_cols])
|
|
|
|
cursor_source.execute(f"SELECT {columns_str} FROM {source_table}")
|
|
|
|
batch_size = 1000
|
|
copied = 0
|
|
|
|
while True:
|
|
rows = cursor_source.fetchmany(batch_size)
|
|
if not rows:
|
|
break
|
|
|
|
cursor_target.executemany(
|
|
f"INSERT OR REPLACE INTO {target_table} ({columns_str}) VALUES ({placeholders})",
|
|
rows
|
|
)
|
|
copied += len(rows)
|
|
print(f"Kopiert: {copied}/{total_rows} Datensätze...", end='\r')
|
|
|
|
conn_target.commit()
|
|
print(f"\n\n✓ Erfolgreich {copied} Datensätze kopiert!")
|
|
return True
|
|
|
|
except sqlite3.Error as e:
|
|
print(f"\nFehler beim Kopieren: {e}")
|
|
return False
|
|
|
|
finally:
|
|
conn_source.close()
|
|
conn_target.close()
|
|
|
|
|
|
def main():
|
|
print("=" * 60)
|
|
print("SQLite Datenkopier-Programm")
|
|
print("=" * 60)
|
|
|
|
# Eingaben
|
|
if len(sys.argv) >= 3:
|
|
source_db = sys.argv[1]
|
|
target_db = sys.argv[2]
|
|
source_table = sys.argv[3] if len(sys.argv) >= 4 else None
|
|
target_table = sys.argv[4] if len(sys.argv) >= 5 else 'weather_data'
|
|
else:
|
|
source_db = input("\nPfad zur Quelldatenbank: ").strip()
|
|
target_db = input("Pfad zur Zieldatenbank: ").strip()
|
|
source_table = input("Name der Quelltabelle (leer=automatisch ermitteln): ").strip() or None
|
|
target_table = input("Name der Zieltabelle [weather_data]: ").strip() or 'weather_data'
|
|
|
|
# Validierung
|
|
if not Path(source_db).exists():
|
|
print(f"\nFehler: Quelldatenbank '{source_db}' nicht gefunden!")
|
|
sys.exit(1)
|
|
|
|
# Kopieren
|
|
success = copy_data(source_db, target_db, source_table, target_table, COLUMNS)
|
|
|
|
if success:
|
|
print(f"\nZieldatenbank: {target_db}")
|
|
print(f"Zieltabelle: {target_table}")
|
|
|
|
sys.exit(0 if success else 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|