-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_parquet_exporter.py
More file actions
57 lines (47 loc) · 1.75 KB
/
Copy pathsql_parquet_exporter.py
File metadata and controls
57 lines (47 loc) · 1.75 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
57
"""
Script: Exports SQL Server query parquet file
Author: M. Carr
Date: 2024-01-07
Description: This program connects to SQL Server, runs a query, and saves the results to a user specified parquet file.
Python package requirements: pyodbc, pyarrow, os
"""
import pyodbc
import pyarrow as pa
import pyarrow.parquet as pq
import os
# Prompt the user for connection parameters
server = input("Enter the SQL Server instance name: ")
database = input("Enter the database name: ")
query = input("Enter the SQL query: ")
output_file = input("Enter the output file name with extension: ")
output_path = input("Enter the output file path: ")
output_file_full_path = os.path.join(output_path, output_file)
try: # Establishing the connection
conn = pyodbc.connect(
'DRIVER={ODBC Driver 17 for SQL Server};'
f'SERVER={server};'
f'DATABASE={database};'
'Trusted_Connection=yes;'
)
# Execute the query and save the result to a list of tuples
cursor = conn.cursor()
print("Query running")
cursor.execute(query)
rows = cursor.fetchall()
print("Query complete")
columns = [column[0] for column in cursor.description]
table = pa.Table.from_arrays([pa.array(column) for column in zip(*rows)], columns)
try:
# Save the pyarrow Table to a Parquet file
pq.write_table(table, output_file_full_path)
print(f"Query results have been saved to {output_file_full_path}")
except Exception as e:
print("Error: The output file could not be saved:")
print(e)
except pyodbc.Error as e:
print("Database Error:")
print(e)
finally:
if 'conn' in locals() or 'conn' in globals():
conn.close()
print("Database connection closed.")