我有一个奇怪的问题。我在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__'
奇怪的是数据库中有一个值 - &gt; 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
答案 0 :(得分:0)
正如你所说,它工作正常,但现在却没有,我能想到的唯一原因是当你的查询返回零结果时。
查看您的代码,如果您的查询只返回一个结果,那么它将打印结果,并且当您下次调用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"
让我知道这是否有效。