简体   繁体   English

如何在read_sql中传递元组'在pandas python中的'子句中

[英]How to pass tuple in read_sql 'where in' clause in pandas python

I am passing a tuple converted to a string in a read_sql method as 我传递一个元组转换为read_sql方法中的字符串作为

sql = "select * from table1 where col1 in " + str(tuple1) + " and col2 in " + str(tuple2)

df = pd.read_sql(sql, conn)

This is working fine but, when tuple have only one value sql fails with ORA-00936: missing expression, as single element tuple has an extra comma 这工作正常,但是,当元组只有一个值sql失败时,ORA-00936:缺少表达式,因为单个元素元组有一个额外的逗号

For example 例如

tuple1 = (4011,)
tuple2 = (23,24)

sql formed is as sql形成的是

select * from table1 where col1 in (4011,) + " and col2 in (23,24)
                                        ^
ORA-00936: missing expression

Is there any better way doing this, other than removal of comma with string operations? 除了用字符串操作删除逗号之外,还有更好的方法吗?

Is there a better way to paramatrize read_sql function? 是否有更好的方法来对read_sql函数进行paramatrize?

There might be a better way to do it but I would add an if statement around making the query and would use .format() instead of + to parameterise the query. 可能有更好的方法来做,但我会添加一个if语句来进行查询,并使用.format()而不是+来参数化查询。

Possible if statement: 可能的if语句:

if len(tuple1) < 2:
    tuple1 = tuple1[0]

This will vary based on what your input is. 这将根据您的输入而有所不同。 If you have a list of tuples you can do this: 如果你有一个元组列表,你可以这样做:

tuples = [(4011,), (23, 24)]
new_t = []
for t in tuples:
    if len(t) == 2:
         new_t.append(t)
    elif len(t) == 1:
         new_t.append(t[0])

Ouput: 输出继电器:

[4011, (23, 24)]

Better way of parameterising querys using .format() : 使用.format()更好地参数化查询的方法:

sql = "select * from table1 where col1 in {} and col2 in {}".format(str(tuple1), str(tuple2))

Hope this helps! 希望这可以帮助!

the reason you're getting the error is because of SQL syntax. 您收到错误的原因是SQL语法。

When you have a WHERE col in (...) list, a trailing comma will cause a syntax error. WHERE col in (...)列表中有WHERE col in (...) ,尾随逗号将导致语法错误。

Either way, putting values into SQL statements using string concatenation is frowned upon, and will ultimately lead you to more problems down the line. 无论哪种方式,使用字符串连接将值放入SQL语句都是不受欢迎的,并且最终会引发更多问题。

Most Python SQL libraries will allow for parameterised queries. 大多数Python SQL库都允许参数化查询。 Without knowing which library you're using to connect, I can't link exact documentation, but the principle is the same for psycopg2: 在不知道您使用哪个库进行连接的情况下,我无法链接确切的文档,但psycopg2的原理是相同的:

http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries

This functionality is also exposed in pd.read_sql , so to acheive what you want safely , you would do this: 此功能也在pd.read_sqlpd.read_sql ,因此要实现您想要的安全 ,您可以这样做:

sql = "select * from table1 where col1 in %s and col2 in %s"

df = pd.read_sql(sql, conn, params = [tuple1, tuple2])
select * from table_name where 1=1 and (column_a, column_b) not in ((28,1),(25,1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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