Good morning;
I created a first python script which saves web data in a .csv file. The latter works perfectly.
I am at the stage of creating a second script which will be responsible for updating a
LibreOffice base table. This script is launched from an LO form.
When I run the script from LO, I get no feedback on the errors encountered.
Additionally, while the base is open, I end up with three identical bases open!!!
The table is not modified. This is probably due to poor writing of the SQL query.
Could you tell me what is not working.
The codeSincerely
I created a first python script which saves web data in a .csv file. The latter works perfectly.
I am at the stage of creating a second script which will be responsible for updating a
LibreOffice base table. This script is launched from an LO form.
When I run the script from LO, I get no feedback on the errors encountered.
Additionally, while the base is open, I end up with three identical bases open!!!
The table is not modified. This is probably due to poor writing of the SQL query.
Could you tell me what is not working.
The code
Code:
#!/user/bin/env python3# Mise à jour d' une table de LibreOffice Basefrom pathlib import Pathimport pyunoimport csvfichierCsv = '/mnt/El_Casot/Yvon/Finance/Bourse/conseils.csv'nmTable = 'T_Ttr'def importCsv(event=True): desktop = XSCRIPTCONTEXT.getDesktop() db_path = Path('/mnt/El_Casot/Yvon/Finance/Bourse/BDD/Archive/Bourse-2327.odb') # Charger le document de base de données db_doc = desktop.loadComponentFromURL(f"file://{db_path.as_posix()}", "_blank", 0, ()) # Obtenez le gestionnaire de services et DriverManager try: manager = pyuno.getComponentContext().getServiceManager() connection = driver_manager.getConnectionWithInfo(f"jdbc:ucanaccess://{db_path.as_uri()}", {}) except Exception as e: print(f"Erreur de connexion à la base de données: {e}") return # Créer une déclaration stmt = connection.createStatement() try: with open(fichierCsv, 'r', encoding='utf-8') as f: reader = csv.DictReader(f) for row in reader: # Afficher la ligne en cours de traitement print(f"Ligne en cours de traitement: {row}") sql = f"UPDATE (nmTable) SET 'Objectif' = row['csvObjectif'], 'DtObjectif' = row['csvDtObjectif']\ WHERE 'Cf_Ttr' = row['csvCfTitre']" try: rows_affected = stmt.executeUpdate(sql) # Afficher la requête et les lignes concernées print(f"SQL: {sql} -> Ligne en cours: {rows_affected}") except Exception as e: print(f"Error executing SQL: {e}") except Exception as e : print(f"Erreur de lecture du fichier CSV: {e}") finally: stmt.close() connection.close() db_doc.store() db_doc.close()g_exportedScripts = (importCsv(),)
Statistics: Posted by El Catala — Wed Sep 18, 2024 8:25 pm