首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >python Msql与fetchone的连接出现错误

python Msql与fetchone的连接出现错误
EN

Stack Overflow用户
提问于 2016-12-07 16:57:19
回答 1查看 107关注 0票数 0

我有个奇怪的问题。我在excel中编写了一段vba代码,它调用python代码从excel表格中获取信息并将其放入数据库中。昨天没有问题。今天,我启动了我的计算机,并尝试了vba代码和它在python文件中的错误。

错误:

代码语言:javascript
运行
复制
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中有一个值...

我的代码:

代码语言:javascript
运行
复制
#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
EN

Stack Overflow用户

发布于 2016-12-07 17:10:18

正如你所说的,它工作得很好,但现在不是了,我能想到的唯一原因是当你的查询返回零结果时。

查看您的代码,如果您的查询只返回一个结果,那么它将打印结果,下一次调用cursorOpenShark.fetchone()以存储在TestchipID中时,它将返回None。

相反,您是否可以尝试以下代码

代码语言:javascript
运行
复制
#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"

如果有效,请让我知道。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41013101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档