How do I pass multiple parameters via pd.read_sql, one singular and another in list format?

五迷三道 提交于 2021-01-25 03:55:17

问题


I have raw data as:

id = 2345  
id_num = 3,6,343,32  

I need to pass both the above as parameters in an ORACLE SQL query via a cx_Oracle connection as:

query = “””  
        select * from mytable where pid = 2345 and id_num in (3,6,343,32)  
        “””  

I am creating a dictionary as:

sparm = {}  
sparm['pid'] = id  
sparm['idnum'] = id_num  

and trying to use it as:

query = “””  
        select * from mytable where pid = :pid and id_num in :idnum  
        “””  

df = pd.read_sql(query, con=conct, params=sparm)  

without success.
The :pid works but the :idnum doesn’t. Any suggestions would be much appreciated.


回答1:


I have raw data as:

id = 2345  
id_num = 3,6,343,32  

I need to pass both the above as parameters in an ORACLE SQL query via a cx_Oracle connection as:

query = “””  
        select * from mytable where pid = 2345 and id_num in (3,6,343,32)  
        “””  

I am creating a dictionary as:

sparm = {}  
sparm['pid'] = id 

and create a tuple for the where clause as:

where=tuple(list(id_num.split(","))) 

and trying to use it as:

query = “””  
        select * from mytable where pid = :pid and id_num in {}  
        “””.format(where)

df = pd.read_sql(query, con=conct, params=sparm)  

with success. The :pid works with a dict input and the :idnum works as a tuple input.



来源:https://stackoverflow.com/questions/57399784/how-do-i-pass-multiple-parameters-via-pd-read-sql-one-singular-and-another-in-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!