Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass table name in read_sql_query

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

You need to change this:

sq = "SELECT * FROM {}"

to this:

sq = "SELECT * FROM `{}`"

If you don’t, the table name won’t be quoted, and it will only associate the first word of "Site Visit Fees-Zero Amount" with the table name, leaving it with extra words. That causes a syntax error.

As a side note, you may want to consider not using spaces inside your table names.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading