i'm trying to create a function for this code:
db_path = dmttools.paths.payments_db
try:
engine = dmttools.database.access_engine(db_path)
access_zeroDollar = pd.read_sql_query("SELECT * FROM `Site Visit Fees-Zero Amount`", engine)
logger.info('Read.')
except Exception as e:
logger.error('Error reading database.')
logger.error(e)
raise e
finally:
engine.dispose()
running this block works fine, problem is...I run multiple times and want to create a function to clean up my script. When I convert to this function:
def read_database(logger, db_path, table, df_name):
logger = dmttools.util.DefaultLogger(logger)
logger.info('Reading existing database...')
sq = "SELECT * FROM {}"
try:
engine = dmttools.database.access_engine(db_path)
df_name = pd.read_sql_query(sq.format(table), engine)
logger.info('Read.')
except Exception as e:
logger.error('Error reading database.')
logger.error(e)
raise e
finally:
engine.dispose()
and call the function like this:
read_database('Zero Dollar Fee Aging Report', dmttools.paths.payments_db, 'Site Visit Fees-Zero Amount', 'access_test')
I receive this error:
ProgrammingError: (pyodbc.ProgrammingError) ('42000', '[42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error in FROM clause. (-3506) (SQLExecDirectW)')
[SQL: SELECT * FROM Site Visit Fees-Zero Amount]
(Background on this error at: http://sqlalche.me/e/13/f405)