-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_connector.py
69 lines (52 loc) · 1.96 KB
/
sql_connector.py
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
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 26 18:34:12 2024
@author: JSR
"""
from sqlalchemy import create_engine, text
import pandas as pd
# Global configuration file path
CNF_FILE_PATH = "config.cnf"
def parse_cnf_file(cnf_file_name):
"""
Parses a configuration file in INI format and returns a dictionary.
Args:
cnf_file_name (str): Path to the configuration file.
Returns:
dict: A dictionary containing the parsed configuration.
"""
config_dict = {}
current_section = None
with open(cnf_file_name, 'r') as file:
for line in file:
line = line.strip()
if line.startswith('#') or not line: # Skip comments and empty lines
continue
if line.startswith('[') and line.endswith(']'):
current_section = line[1:-1].strip()
config_dict[current_section] = {}
elif '=' in line and current_section is not None:
key, value = line.split('=', 1)
config_dict[current_section][key.strip()] = value.strip() # Remove spaces
return config_dict
def read_query(query):
"""
Executes a SQL query using configuration parameters from a file and returns the results as a DataFrame
Args:
query (str): The SQL query to execute.
Returns:
pd.DataFrame: DataFrame containing the query results.
"""
sc = parse_cnf_file(CNF_FILE_PATH)
try:
host = sc['local']['host']
username = sc['local']['username']
password = sc['local']['password']
except KeyError as e:
raise KeyError(f"Configuration error: {e} is missing in the config file.")
# Create a database connection
engine = create_engine(f"mysql+pymysql://{username}:{password}@{host}")
with engine.connect() as connection:
result = connection.execute(text(query))
df = pd.DataFrame(result.fetchall(), columns=result.keys())
return df