Nonetype对象没有属性' id'

时间:2014-03-28 05:58:31

标签: python attributes cursor

我正在从mssql中检索最后一个id并尝试递增它并存储带有id的fie名称..但是我得到“属性错误:Nonetype对象没有属性'id'”..代码和错误到这里:

import Tkinter,tkFileDialog 
import shutil    
import pyodbc    
cnxn = pyodbc.connect("DRIVER={SQL Server};SERVER=PAVAN;DATABASE=video;Trusted_Connection=yes;")   
cursor = cnxn.cursor()  
cursor.execute("SELECT TOP 1 id  FROM files ORDER BY id DESC ")

while 1:   
    row = cursor.fetchone()  
    if not row:   
        break   
    print row.id    
cnxn.close()    
middle = Tkinter.Tk()

def withdraw():    
   dirname = tkFileDialog.askopenfilename(parent=middle,initialdir="H:/",title='Please 
select a file')    
   a="H:python(test)\py_"+row.id+".mp4"    
   b=shutil.copyfile(dirname,a)

   if b!="":    
       print "Successfully Copied"    
   else:    
       print "Could not be copied"

B = Tkinter.Button(middle, text ="UPLOAD", command = withdraw)    
middle.geometry("450x300+100+100")    
middle.configure(background="black")    
B.pack() 
middle.mainloop() 

我得到的错误是:

Traceback (most recent call last):  
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
      return self.func(*args)
  File "C:\Users\hp\Desktop\upload.py", line 20, in withdraw
    a="H:python(test)\py_"+row.id+".mp4"
AttributeError: 'NoneType' object has no attribute 'id'

1 个答案:

答案 0 :(得分:1)

当您尝试从无类型的对象获取 id 属性时,会发生这种情况, 这是一个案例:

>> a = None
>> a.id
AttributeError: 'NoneType' object has no attribute 'id'

因此可能是的案例对象属于无类型,并且您正在尝试打印row.id

您可以使用以下方式检查行的类型:

type(**row**)

关心 迪帕克