resourceclosederror/attributeerror当使用perdas read\u sql\u query attributeerror时:“nonetype”对象没有属性“fetchall”

umuewwlo  于 2021-08-01  发布在  Java
关注(0)|答案(1)|浏览(492)

我正在尝试使用pandas.read\u sql\u query读取sql查询。
几周前我还没谈到这些问题。问题是,当我阅读一些查询时,会出现以下错误:

df = pd.read_sql_query(sql = query_string, con = engine)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\.conda\envs\test\lib\site-packages\sqlalchemy\engine\result.py in _fetchall_impl(self)
   1160         try:
-> 1161             return self.cursor.fetchall()
   1162         except AttributeError:

AttributeError: 'NoneType' object has no attribute 'fetchall'

During handling of the above exception, another exception occurred:

ResourceClosedError                       Traceback (most recent call last)
...
~\.conda\envs\test\lib\site-packages\sqlalchemy\engine\result.py in _non_result(self, default)
   1166         if self._metadata is None:
   1167             raise exc.ResourceClosedError(
-> 1168                 "This result object does not return rows. "
   1169                 "It has been closed automatically."
   1170             )

ResourceClosedError: This result object does not return rows. It has been closed automatically.

这只发生在使用某些查询时,而不是其他查询时,所以我知道这不是我的连接。我在这两个语句上都设置了nocount,并且都使用temp表。有人能告诉我为什么一个查询有效,而另一个查询无效吗?两者都在ssms中工作良好。我试着在下面提供这两种语言的基本结构。第一个有效,第二个无效。

set nocount on

DECLARE      @var1  varchar(20)= 'sometext'                                                             

IF OBJECT_ID('tempdb..#temp1', 'U') IS NOT NULL                                                                     
        DROP TABLE #temp1                                                                       

select * into  #temp1 from  some_function_view WHERE  [x] = @var1                                                                       

IF OBJECT_ID('tempdb..#temp2', 'U') IS NOT NULL                                                                     
        DROP TABLE #temp2                                                                       
select * into #temp2 FROM another_view JOIN ...                                                     

SELECT Main.[y]...                                                                                                                                      
FROM (                                                                      

    SELECT *                                                                                                                                    
    FROM another_view                                                                       
    JOIN...                                                                                                                                     
    WHERE...                                                                
    GROUP BY...
) Main                                                                      
    JOIN #temp2...
set nocount on

begin try drop table #temp1 end try begin catch end catch
begin try drop table #temp2 end try begin catch end catch
begin try drop table #temp3 end try begin catch end catch

DECLARE @x  int = 202007
DECLARE @y  int = 2016  

SELECT * into #temp1 FROM a_view

SELECT  * into #temp2       
FROM a_view     
JOIN (SELECT...FROM a_view WHERE...GROUP BY ...)

SELECT... into #temp3 FROM a_view       
JOIN... 
LEFT JOIN...
WHERE x = @x, y = @y... 
Group BY...

SELECT *

FROM #policy as FPFR        
    JOIN #temp1...
    JOIN #temp2...
    JOIN #temp3...
ORDER BY...
kqlmhetl

kqlmhetl1#

所以我想出来了。我需要在sql查询中包含“setansi\u warnings off”和“setnocount on”。
有一个或另一个是不够的。两者的结合能够捕获sql抛出的任何存储过程错误,以便python能够成功地读取它。

相关问题