【问题标题】:Panda.run_sql_query for where .. in .. structure?Panda.run_sql_query for where .. in .. 结构?
【发布时间】:2019-11-10 10:26:12
【问题描述】:

我有一个结构为 where a in b 的 sql 查询。我正在尝试使用 run_sql_query 通过 pandas 运行它以获取数据帧。但似乎没有数据结构适用于熊猫查询。我应该有什么让它工作?

sql:

Select * from Table where a in (:input)

Python:

conn = cx-oracle.connect(....)

df = pd.run_sql_query(sql,conn, params= {'input':('A','B')})

错误信息:

cx_Oracle.NotSupportedError: Python value of type tuple not supported.

我尝试了 numpy 数组和列表,但似乎都不起作用。这里应该使用什么数据结构?

【问题讨论】:

    标签: python sql pandas cx-oracle


    【解决方案1】:

    试试这个:

    myinput = ('a','b','c')
    myinput = str(myinput)
    #Now your query will end up like
    # select * from tablename where value in ('a','b','c')
    df = pd.run_sql_query(sql,conn, params= {'input':myinput})
    

    【讨论】:

    • 兄弟使用三引号,像这样: {'input':"' 'A' , 'B' '"}
    • 我该怎么做?在这种情况下,有一个包含 2 个元素的元组。我现在一直在做的是加入字符串,如果我再添加 1 个引号:就像 " ' 'A' ,'B' ' " 并且还不起作用
    • 尝试修改我的代码,然后尝试。我看到你是手动做的,这不是一个好习惯。将元组类型转换为字符串并在查询中使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 2016-11-15
    相关资源
    最近更新 更多