2017-12-06 3 views
0

현재 시퀀스에서 첫 번째 질의에 다음과 같은 오류가 발생, (MS sql server에서) SQL 쿼리의 순서를 읽고 dataframe으로 검토 pandaspyodbc를 사용하려고하고 :실행 순서

sql_in = """ 
SET NOCOUNT ON; 
select <stuff> from <table1> into #<temptable1> 
----SPLIT 
SET NOCOUNT ON; 
select <stuff> from <table2> into #<temptable2> 
----SPLIT 
SET NOCOUNT ON; 
select <stuff> from <table3> into #<temptable3> 
----SPLIT 
SET NOCOUNT ON; 
select <stuff> from <table4> <joined with temptables1-3> into #<temptable4> 
""".split('----SPLIT') 

for count, q in enumerate(sql_in): 
    print count 
    frame_in = pd.read_sql(q, cnxn) 

최종 임시 테이블에 있기 때문에에만 관심이 방법을 완료 조언은 0을 기록 때문에 (split를 사용 : 같은

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-95-5eec1638614b> in <module>() 
    285 for count, q in enumerate(sql_in): 
    286  print count 
--> 287  frame_in = pd.read_sql(q, cnxn) 
    288 print frame_in.head(n=10) 
    289 

/home/mapr/anaconda2/lib/python2.7/site-packages/pandas/io/sql.pyc in read_sql(sql, con, index_col, coerce_float, params, parse_dates, columns, chunksize) 
    497    sql, index_col=index_col, params=params, 
    498    coerce_float=coerce_float, parse_dates=parse_dates, 
--> 499    chunksize=chunksize) 
    500 
    501  try: 

/home/mapr/anaconda2/lib/python2.7/site-packages/pandas/io/sql.pyc in read_query(self, sql, index_col, coerce_float, params, parse_dates, chunksize) 
    1598   args = _convert_params(sql, params) 
    1599   cursor = self.execute(*args) 
-> 1600   columns = [col_desc[0] for col_desc in cursor.description] 
    1601 
    1602   if chunksize is not None: 

TypeError: 'NoneType' object is not iterable 

관련 코드가 보인다).

cnxn = pyodbc.connect(cnxn_str) 
cursor = cnxn.cursor() 

#Sample select query 
print 'Testing db connection...' 
cursor.execute("SELECT @@version;") 
row = cursor.fetchone() 
while row: 
    print row[0] 
    row = cursor.fetchone() 

을 그리고 실제로 서버 버전을 받고 :

그 pyodbc을 확인할 수 있습니다 실행하여 바로 구성되어 있습니다.

왜 이런 일이 발생할 수 있고 수정하는 방법에 대한 아이디어가 있습니까? 감사.

+0

DB-API는'.execute()'가 명령 하나당 하나의 쿼리 만 허용한다고 지정합니다. – bernie

+0

@bernie - 방금 [PEP249] (https://www.python.org/dev/peps/pep-0249/)를 다시 한 번 보았습니다. 물론 익명 코드 블록 (다중 문장 일괄 처리) * ODBC 드라이버가 허용하는 경우 (SQL Server 용 Microsoft의 ODBC 드라이버가 허용하는 경우) * 작동 할 수 있습니다. –

+0

아, 그래. 고맙습니다 @ GordThompson. 나는 그때 착각해야한다. – bernie

답변

1

처음 몇 개의 쿼리가 데이터를 반환하지 않기 때문입니다. Pandas read_sql은 데이터가 다시 오기를 기대합니다. 이러한 쿼리를 실행하려면 pyodbc execute 함수를 사용해야합니다.

+0

... 아니면 익명의 코드 블록 전체를'read_sql'으로 넘기지 않으시겠습니까? (지금 당장 시도 할 수 없으며, 내 폰에 팬더가 없다 ....) –

+0

그 코드는 더 잘 작동 할 것이고, 나는 왜 코드가 작동하지 않는지에 대한 질문에 대답하려하고있다. – scomes

+0

이 포스트는 왜 원래의 질문에 대한 대답인가? 코드는 작동하지 않았지만 궁극적으로 사용자 GordThompson이 제안한 솔루션을 구현하지 못했습니다. 둘 다 고마워. – lampShadesDrifter