inserimento massivo
This commit is contained in:
14
.gitignore
vendored
Normal file
14
.gitignore
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
.python-version
|
||||
DT0028_20241106044856.csv
|
||||
DT0029_20241106044856.csv
|
||||
DT0030_20241106104859.csv
|
||||
input_file.txt
|
||||
README.md
|
||||
uv.lock
|
||||
csv_ase.egg-info/dependency_links.txt
|
||||
csv_ase.egg-info/PKG-INFO
|
||||
csv_ase.egg-info/requires.txt
|
||||
csv_ase.egg-info/SOURCES.txt
|
||||
csv_ase.egg-info/top_level.txt
|
||||
dist/csv_ase-0.1.0-py3-none-any.whl
|
||||
dist/csv_ase-0.1.0.tar.gz
|
||||
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"CodeGPT.apiKey": "Ollama"
|
||||
}
|
||||
@@ -20,6 +20,7 @@ if len(lines) >= 7:
|
||||
else:
|
||||
raise ValueError("Il file non contiene abbastanza righe per estrarre i dati richiesti.")
|
||||
|
||||
records = []
|
||||
# Elabora le righe dei dati a partire dalla riga 8 in poi
|
||||
for line in lines[7:]:
|
||||
# Rimuovi spazi bianchi o caratteri di nuova riga
|
||||
@@ -41,7 +42,6 @@ for line in lines[7:]:
|
||||
nodes = parts[2:]
|
||||
|
||||
node_list = []
|
||||
records = []
|
||||
for i, node_data in enumerate(nodes, start=1):
|
||||
# Dividi ogni nodo in valori separati da ";"
|
||||
node_values = node_data.split(';')
|
||||
@@ -71,14 +71,17 @@ for line in lines[7:]:
|
||||
"temperature": float(measurements.split(';')[1]),
|
||||
"nodes_jsonb": json.dumps(node_list) # Converti la lista di dizionari in una stringa JSON
|
||||
}
|
||||
|
||||
records.append(record)
|
||||
|
||||
# Esegui l'upsert con un comando SQL
|
||||
with engine.connect() as conn:
|
||||
with conn.execution_options(isolation_level='AUTOCOMMIT'):
|
||||
result = conn.execute(text("""
|
||||
conn.execute(text("""
|
||||
INSERT INTO dataraw (tipo_centralina, unit, ip_centralina, path, ip_gateway, event_timestamp, battery_level, temperature, nodes_jsonb)
|
||||
VALUES (:tipo_centralina, :unit, :ip_centralina, :path, :ip_gateway, :event_timestamp, :battery_level, :temperature, :nodes_jsonb)
|
||||
VALUES
|
||||
""" + ",".join([
|
||||
f"(:{i}_tipo_centralina, :{i}_unit, :{i}_ip_centralina, :{i}_path, :{i}_ip_gateway, :{i}_event_timestamp, :{i}_battery_level, :{i}_temperature, :{i}_nodes_jsonb)"
|
||||
for i in range(len(records))
|
||||
]) + """
|
||||
ON CONFLICT ON CONSTRAINT dataraw_unique
|
||||
DO UPDATE SET
|
||||
path = EXCLUDED.path,
|
||||
@@ -86,7 +89,8 @@ with engine.connect() as conn:
|
||||
battery_level = EXCLUDED.battery_level,
|
||||
temperature = EXCLUDED.temperature,
|
||||
nodes_jsonb = EXCLUDED.nodes_jsonb;
|
||||
"""), records)
|
||||
print(result)
|
||||
"""), {f"{i}_{key}": value for i, record in enumerate(records) for key, value in record.items()})
|
||||
|
||||
conn.commit()
|
||||
|
||||
print("Tutte le righe del file sono state caricate con successo nella tabella PostgreSQL!")
|
||||
|
||||
Reference in New Issue
Block a user