我有个奇怪的问题。我在excel中编写了一段vba代码,它调用python代码从excel表格中获取信息并将其放入数据库中。昨天没有问题。今天,我启动了我的计算机,并尝试了vba代码和它在python文件中的错误。
错误:
testchipnr = TC104
Traceback (most recent call last):
testchipID = 108
File "S:/3 - Technical/13 - Reports & Templates/13 - Description/DescriptionToDatabase/DescriptionToDatabase.py", line 40, in <module>
TestchipID = cursorOpenShark.fetchone()[0] # Fetch a single row using fetchone() method and store the result in a variable., the [0] fetches only 1 value
TypeError: 'NoneType' object has no attribute '__getitem__'奇怪的是,在数据库-> testchipID中有一个值...
我的代码:
#get the testchipID and the testchipname
testchipNr = sheet.cell(7, 0).value # Get the testchipnr
print "testchipnr = ", testchipNr
queryTestchipID = """SELECT testchipid FROM testchip WHERE nr = '%s'""" %(testchipNr)
cursorOpenShark.execute(queryTestchipID)
print "testchipID = ", cursorOpenShark.fetchone()[0]
TestchipID = cursorOpenShark.fetchone()[0] # Fetch a single row using fetchone() method and store the result in a variable., the [0] fetches only 1 value发布于 2016-12-07 17:10:18
正如你所说的,它工作得很好,但现在不是了,我能想到的唯一原因是当你的查询返回零结果时。
查看您的代码,如果您的查询只返回一个结果,那么它将打印结果,下一次调用cursorOpenShark.fetchone()以存储在TestchipID中时,它将返回None。
相反,您是否可以尝试以下代码
#get the testchipID and the testchipname
testchipNr = sheet.cell(7, 0).value # Get the testchipnr
print "testchipnr = ", testchipNr
queryTestchipID = """SELECT testchipid FROM testchip WHERE nr = '%s'""" %(testchipNr)
cursorOpenShark.execute(queryTestchipID)
TestchipID = cursorOpenShark.fetchone()[0] # Fetch a single row using fetchone() method and store the result in a variable., the [0] fetches only 1 value
if TestchipID:
print "testchipID = ", TestchipID[0]
else:
print "Returned nothing"如果有效,请让我知道。
https://stackoverflow.com/questions/41013101
复制相似问题