NoneType对象没有'id'属性
我正在从mssql数据库中获取最后一个id,然后想把它加1,并把文件名和这个id存起来。但是我遇到了一个错误:“属性错误: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 个回答
1
这个问题发生在你试图从一个值为 None 的对象中获取 id 属性时。举个例子:
>> a = None
>> a.id
AttributeError: 'NoneType' object has no attribute 'id'
所以可能是对象 row 的值是 None,而你却在尝试打印 row.id。
你可以用下面的方法检查 row 的类型:
type(**row**)
此致,
Deepak