Goal: To access a database of hundreds of tables, iterate through each table's columns to find a specified column name and store the name of the table in a list.
Overview: I setup access to a database and stored the names of all the tables in a variable.
import pandas
import pyodbc
main_path = 'my_database_name'
with pyodbc.connect("DRIVER={SQL Server};" +
"SERVER=MyServerName;" +
f"DATABASE={main_path};" +
"Trusted_Connection=yes;") as main_conn:
db_table_names = [_.table_name for _ in main_conn.cursor().tables()]
I then tried to iterate through the list of table names, retrieve the table and see if the column name I'm looking for is in the columns.
col_name ='whatever_col_i_need'
table_matches = [table_name if col_name in pandas.read_sql(f"SELECT * FROM {table_name }",
con=main_conn).columns else None for table in db_tables]
Problem: I think this sucks up a lot of resources. It may be 'holding on' to each of the tables as it iterates, or is just generally slow. I thought about using a with
statement so it would connect to the table, look for the column, then disconnect from the table, but I'm not sure if that's the best way, plus I don't think I'm using the right format.
found_tables = []
for table_name in db_table_names:
with pandas.read_sql(f"SELECT * FROM {table_name}", con=main_conn) as temp_tbl:
if 'LOCATION' in temp_tbl.columns:
found_tables.append(table_name)
UPDATE: I figured out a method that is super fast and meets my needs perfectly.
Since I don't really care what information is in the table and just want to confirm that the column name exists, there's no need to store the table data, even if temporarily. By setting up a try/except and having a query to get a column, if the column doesn't exist in the table then it hits the except and loops.
found_tables = []
for table_name in db_tables:
try:
# Print statement to just follow the progress
print(f'Checking: {table_name} for {col_name}...')
pandas.read_sql_query(f"SELECT {col_name} FROM {table_name}", con=main_conn)
found_tables.append(table_name)
except Exception as e:
print(e)
pass