我正在将一个转换为字符串的元组作为参数传递给read_sql方法
sql = "select * from table1 where col1 in " + str(tuple1) + " and col2 in " + str(tuple2)
df = pd.read_sql(sql, conn)
这段代码本来是正常运行的,但当元组只包含一个值时,SQL会出现ORA-00936: missing expression错误,因为这样的单元素元组有一个额外的逗号。例如:
tuple1 = (4011,)
tuple2 = (23,24)
SQL语句的格式如下:
select * from table1 where col1 in (4011,) + " and col2 in (23,24)
^
ORA-00936: missing expression
除了使用字符串操作去掉逗号之外,有没有更好的方法来做这件事?
有没有更好的方法来参数化read_sql函数?