-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_tables_module.py
More file actions
56 lines (46 loc) · 2.6 KB
/
Copy pathexport_tables_module.py
File metadata and controls
56 lines (46 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import ibm_db
import csv
def export_tables(conn_str, output_dir):
try:
# Connect to the database
conn = ibm_db.connect(conn_str, "", "")
if conn:
print("Connected to the database.")
# Execute the SQL query to enumerate schemas
schema_query = "SELECT SCHEMANAME FROM SYSCAT.SCHEMATA"
schema_stmt = ibm_db.exec_immediate(conn, schema_query)
# Fetch and iterate over schema names
schema_row = ibm_db.fetch_tuple(schema_stmt)
while schema_row:
schema_name = schema_row[0]
print("Exporting tables in schema:", schema_name)
# Execute the SQL query to enumerate tables in the current schema
tables_query = f"SELECT TABNAME FROM SYSCAT.TABLES WHERE TABSCHEMA = '{schema_name}'"
tables_stmt = ibm_db.exec_immediate(conn, tables_query)
# Fetch table names
table_row = ibm_db.fetch_tuple(tables_stmt)
while table_row:
table_name = table_row[0]
print(" Exporting table:", table_name)
try:
# Execute the SQL query to export data from the current table
data_query = f"SELECT * FROM {schema_name}.{table_name}"
data_stmt = ibm_db.exec_immediate(conn, data_query)
# Write data to CSV file
with open(f"{output_dir}/{table_name}.csv", 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow([ibm_db.field_name(data_stmt, i) for i in range(ibm_db.num_fields(data_stmt))])
while ibm_db.fetch_row(data_stmt):
writer.writerow(ibm_db.fetch_tuple(data_stmt))
except Exception as e:
# Handle all exceptions
print(f" An error occurred while exporting table {schema_name}.{table_name}: {e}")
table_row = ibm_db.fetch_tuple(tables_stmt) # Move to the next table
schema_row = ibm_db.fetch_tuple(schema_stmt) # Move to the next schema
# Close the connection
ibm_db.close(conn)
print("Database export completed.")
else:
print("Failed to connect to the database.")
except Exception as e:
print("An error occurred:", e)