2013-07-15 1 views
1

내가 pyodbc에서 fetchall() 메서드에서 반환 목록에 열을 추가하려고 해요을 pyodbc,하지만 그것은 나에게 오류를주고있다.추가 열 목록

import pyodbc 
import time 
import calendar 
from datetime import date 

#variable declaration 
today = date.today() 
beginRange = date(today.year,today.month,1) 
endRange = date(today.year,today.month,2) #limit data to 2 days for testing 

#connect to database 
connJobDtl = pyodbc.connect("DSN=Global_MWM;UID=Master") 
cJobDtl = connJobDtl.cursor() 

#database query 
cJobDtl.execute("select job,suffix,seq,date_sequence,[...]") 
dataJobDtl = cJobDtl.fetchall() 
cJobDtl.close() 

#add another column to the list, date_sequence formatted to YYYY-MM 
dataJobDtl = [x + [x[3].strftime("%Y-%m")] for x in dataJobDtl] 

내가 스크립트를 실행할 때이 오류를 받고 있어요 : 여기 내 코드 테스트로

File "...\jobDetailScript.py", line 23, in <module> 
    dataJobDtl = [x + [x[3].strftime("%Y-%m")] for x in dataJobDtl] 
TypeError: unsupported operand type(s) for +: 'pyodbc.Row' and 'list' 

을, 나는 파이썬 쉘에서 대표적인 예를 생성하고 잘 작동하지만 수동으로 오히려 fetchall에서 목록을 생성하는 것보다 목록의 목록을 만들어(). 이 오류를 어떻게 해결할 수 있습니까?

답변

1

그것은 매우 간단 보인다 - 오류 메시지가 객체의 + 두 가지 유형에 노력하고 상태로. 방금 목록으로 행을 시전하면 내 자신의 임시 테스트에서, 그래서 작동합니다 : 큰 브래드 일

>>>cur.execute('<removed>') #one of my own tables 
>>>tmp = cur.fetchall() 
>>>type(tmp[0]) #this is KEY! You need to change the type 

<type 'pyodbc.Row'> 

>>>tt = [1,2,3] 
>>>tmp[0] + tt #gives the same error you have 

Traceback (most recent call last): 
    File "<pyshell#13>", line 1, in <module> 
tmp[0] + tt 
TypeError: unsupported operand type(s) for +: 'pyodbc.Row' and 'list' 

>>>list(tmp[0]) + tt #returns a list as you wanted 

[14520496, ..., 1, 2, 3] 
+0

합니다. 도와 주셔서 감사합니다! – emiller3061