From aa86013bf2c444b505e63d19919cd21f9153e9f0 Mon Sep 17 00:00:00 2001 From: Michael Boniface <m.j.boniface@soton.ac.uk> Date: Fri, 21 Feb 2025 16:23:26 +0000 Subject: [PATCH] not dumped all database tables to file on export so they can be versioned --- acmc/omop.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/acmc/omop.py b/acmc/omop.py index 2a0bfd1..7075994 100644 --- a/acmc/omop.py +++ b/acmc/omop.py @@ -186,7 +186,7 @@ def concept_set_exist(cursor, concept_set_name): cursor.execute(query, (concept_set_name,)) # 1 if exists, 0 otherwise - return cursor.fetchone()[0] 1 + return cursor.fetchone()[0] == 1 def export(map_path, export_path, version, omop_metadata): logger.debug(f"exporting with metadata {omop_metadata} at version {version}") @@ -271,6 +271,20 @@ def export(map_path, export_path, version, omop_metadata): df_out["concept_set_id"] = concept_set_id df_out.to_sql("CONCEPT_SET_ITEM", conn, if_exists='append', index=False) + # Output all tables to CSV + # Get the list of all tables + cur.execute("SELECT name FROM sqlite_master WHERE type='table';") + tables = cur.fetchall() # List of tables + + # Export each table to a separate CSV file + for table in tables: + table_name = table[0] # Extract table name + df = pd.read_sql_query(f"SELECT * FROM {table_name}", conn) + output_file = f"{table_name}.csv" + output_path = export_path / output_file + df.to_csv(output_path, index=False) # Save as CSV + logger.info(f"Exported {table_name} to {table_name}.csv") + conn.close() logger.debug(f"Created export db successfully") -- GitLab