简体   繁体   中英

pyodbc/sqlalchemy - read each column in the table using pd.read_sql_query. Pass variable through the query

I want to pass a variable 'single_column' through pd.read_sql_query in loop:

for single_column in columns_list:    
    df_trial_queries = pd.read_sql_query("SELECT single_column FROM dw.db.table;",db_cnxn)

I tried to use something like this:

for single_column in columns_list:    
        df_trial_queries = pd.read_sql_query("SELECT %(column_name)s FROM dw.db.table;",db_cnxn,params = {'column_name':single_column})

No luck at all!

You can't "paremeterize" table or column names in SQL (SQL allows to "parameterize" literals only), but you can easily do it on Python level:

In [25]: single_column = 'col1'

In [52]: table = 'dw.db.table'

In [53]: "SELECT {} FROM {}".format(single_column, table)
Out[53]: 'SELECT col1 FROM dw.db.table'

or in your case:

df_trial_queries = pd.read_sql_query("SELECT {} FROM dw.db.table".format(single_column), db_cnxn)

NOTE: it's very inefficient way! I'm sure there is a better way to achieve your goals, but you would have to shed some light on what are you going to achieve using this loop...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM