diff --git a/src/help.py b/src/help.py index 7c7abad..22c008f 100644 --- a/src/help.py +++ b/src/help.py @@ -1,8 +1,10 @@ def help(): print("\nAvailable Commands:\n") + print("selectDatabase - Lists all databases on your SQL Server and prompts for a selection of database you want to use") print("listDatabases - Lists all Databases in the SQL Server as defined in your sql_config.json") print("listTables - Lists all tables in the Database that's configured in your sql_config.json") - print("productData - Saves a CSV with data for all products to the Output folder with the following information: \n tProduct_ID,Sku,Product_Type,Product_Code,ShortDescription,IsActive") - print("skuCost - Saves a CSV file for each Product Category to the Output folder that contains the items sku and associated cost") + print("listTableData - Lists all the tables in the Database that's configured in your sql_config.json, then prompts\n for a table selection to show the column for that table") + print("outputProductData - Saves a CSV with data for all products to the Output folder with the following information: \n tProduct_ID,Sku,Product_Type,Product_Code,ShortDescription,IsActive") + print("outputSkuCost - Saves a CSV file for each Product Category to the Output folder that contains the items sku and associated cost") print("help - Display available commands") print("exit - Quit the program\n") diff --git a/src/listTableData.py b/src/listTableData.py new file mode 100644 index 0000000..e0f9d13 --- /dev/null +++ b/src/listTableData.py @@ -0,0 +1,57 @@ +import pyodbc +from util.sqlHandler import read_sql_config + +# Read SQL credentials from the JSON file +sql_server, sql_database, sql_user, sql_password, connection_string = read_sql_config() + +# Connect to the database +try: + connection = pyodbc.connect(connection_string) +except pyodbc.Error as e: + print(f"Error connecting to the database: {e}") + exit() + +# Fetch table names from the database +try: + cursor = connection.cursor() + cursor.execute("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE';") + tables = [row.TABLE_NAME for row in cursor.fetchall()] +except pyodbc.Error as e: + print(f"Error fetching table names: {e}") + exit() + +# Display available tables to the user +print("Available Tables:") +for idx, table in enumerate(tables, start=1): + print(f"{idx}. {table}") + +# Ask the user to select a table +try: + table_index = int(input("Enter the number of the table to display its columns: ")) - 1 + if table_index < 0 or table_index >= len(tables): + print("Invalid table number selected.") + exit() + table_name = tables[table_index] +except ValueError: + print("Invalid input. Please enter a number.") + exit() + +# Get the data types of columns in the specified table +columns = [] + +try: + cursor.execute(f"SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ?;", (table_name,)) + rows = cursor.fetchall() + columns = [(row.COLUMN_NAME, row.DATA_TYPE) for row in rows] +except pyodbc.Error as e: + print(f"Error fetching column data types: {e}") + exit() + +# Print the data types of columns +print(f"Data types for table '{table_name}':") +for column_name, data_type in columns: + print(f"{column_name}: {data_type}") + +# Close the cursor and connection +cursor.close() +connection.close() diff --git a/src/productData.py b/src/outputProductData.py similarity index 100% rename from src/productData.py rename to src/outputProductData.py diff --git a/src/skuCost.py b/src/outputSkuCost.py similarity index 100% rename from src/skuCost.py rename to src/outputSkuCost.py diff --git a/src/selectDatabase.py b/src/selectDatabase.py new file mode 100644 index 0000000..c1318fc --- /dev/null +++ b/src/selectDatabase.py @@ -0,0 +1,63 @@ +import json +import pyodbc + +# Function to read SQL config from json +def read_sql_config(): + with open('sql_config.json', 'r') as config_file: + return json.load(config_file) + +# Function to write SQL config to json +def write_sql_config(config_data): + with open('sql_config.json', 'w') as config_file: + json.dump(config_data, config_file, indent=4) + +# Read SQL config from json +sql_config = read_sql_config() + +# Define the connection string +connection_string = ( + f"DRIVER={{ODBC Driver 17 for SQL Server}};" + f"SERVER={sql_config['sql_server']};" + f"DATABASE=master;" + f"UID={sql_config['sql_user']};" + f"PWD={sql_config['sql_password']}" +) + +try: + # Connect to the database + connection = pyodbc.connect(connection_string) + cursor = connection.cursor() + + # Fetch database names + cursor.execute("SELECT name FROM sys.databases WHERE database_id > 4") # Exclude system databases + databases = [row[0] for row in cursor.fetchall()] + + # Display available databases to the user + print("Available Databases:") + for idx, db in enumerate(databases, start=1): + print(f"{idx}. {db}") + + # Ask the user to select a database + try: + db_index = int(input("Enter the number of the database to use: ")) - 1 + if db_index < 0 or db_index >= len(databases): + print("Invalid database number selected.") + exit() + selected_db = databases[db_index] + except ValueError: + print("Invalid input. Please enter a number.") + exit() + + # Update the JSON config with the selected database + sql_config['sql_database'] = selected_db + write_sql_config(sql_config) + + print(f"Selected database updated to '{selected_db}' in sql_config.json.") + +except pyodbc.Error as e: + print(f"Error connecting to the database: {e}") + +finally: + # Close the connection + cursor.close() + connection.close()