将MySQL查询结果存储到Python变量中

3
当我使用Python编程语言和MySQL数据库执行以下代码时:
cursor.execute("select max(propernoun_SRNO) from tblauto_tagged")
starting_index = cursor.fetchone()
ending_index = starting_index +len(s)

我遇到了以下错误:

我收到了以下错误消息:

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
batch(1,1)
File "C:\Users\vchauhan\Dropbox\Code\proper_noun_function_batch_file_mysql_sept_12.py", line 97, in batch
ending_index = starting_index +len(s)
TypeError: unsupported operand type(s) for +: 'pyodbc.Row' and 'int'
1个回答

6

问题

这里的问题是您将.fetchone()返回的pyodbc.Row实例分配给了starting_index,这使得将其添加到整数变量上变得不可能(即出现 "TypeError: unsupported operand type(s)" 错误)。

解决方案

尝试替换以下行:

starting_index = cursor.fetchone()

使用这行代码:
starting_index = cursor.fetchone()[0]

更多阅读


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接