88 lines
1.7 KiB
Python
Executable File
88 lines
1.7 KiB
Python
Executable File
import sqlite3
|
|
from datetime import datetime
|
|
|
|
DB = "nexus.db"
|
|
|
|
FORMATOS = [
|
|
"%m/%d/%y %H:%M:%S",
|
|
"%m/%d/%y"
|
|
]
|
|
|
|
def convertir_fechas(db, tabla, campos):
|
|
|
|
for campo in campos:
|
|
|
|
print(f"\n=== {tabla}.{campo} ===")
|
|
|
|
total = db.execute(
|
|
f"SELECT COUNT(*) FROM {tabla} "
|
|
f"WHERE {campo} IS NOT NULL AND TRIM({campo}) <> ''"
|
|
).fetchone()[0]
|
|
|
|
print(f"Registros con datos: {total}")
|
|
|
|
rows = db.execute(
|
|
f"SELECT id, {campo} FROM {tabla} "
|
|
f"WHERE {campo} IS NOT NULL AND TRIM({campo}) <> ''"
|
|
).fetchall()
|
|
|
|
actualizados = 0
|
|
errores = 0
|
|
|
|
for id_, valor in rows:
|
|
|
|
convertido = False
|
|
|
|
for formato in FORMATOS:
|
|
try:
|
|
fecha = datetime.strptime(valor.strip(), formato)
|
|
nueva = fecha.strftime("%Y-%m-%d")
|
|
|
|
db.execute(
|
|
f"UPDATE {tabla} SET {campo}=? WHERE id=?",
|
|
(nueva, id_)
|
|
)
|
|
|
|
actualizados += 1
|
|
convertido = True
|
|
break
|
|
|
|
except ValueError:
|
|
pass
|
|
|
|
if not convertido:
|
|
print(f"Error -> id={id_} valor='{valor}'")
|
|
errores += 1
|
|
|
|
db.commit()
|
|
|
|
print(f"Actualizados: {actualizados}")
|
|
print(f"Errores: {errores}")
|
|
|
|
|
|
db = sqlite3.connect(DB)
|
|
|
|
# Tabla vehiculos
|
|
convertir_fechas(
|
|
db,
|
|
"vehiculos",
|
|
[
|
|
"f_alta",
|
|
"f_baja",
|
|
"itv_ultima",
|
|
"f_destino_situacion"
|
|
]
|
|
)
|
|
|
|
# Tabla historial
|
|
convertir_fechas(
|
|
db,
|
|
"historial",
|
|
[
|
|
"fecha"
|
|
]
|
|
)
|
|
|
|
db.close()
|
|
|
|
print("\nConversión terminada.") |