简体   繁体   English

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

[英]storing result of MySQL query into python variable

when I execute the following code using python programming language and MySQL database 当我使用python编程语言和MySQL数据库执行以下代码时

cursor.execute("select max(propernoun_SRNO) from tblauto_tagged")
starting_index = cursor.fetchone()
ending_index = starting_index +len(s)

I get following error: 我收到以下错误:

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'

Problem 问题

The problem here is that you are assigning pyodbc.Row instance (returned by .fetchone() ) to starting_index , which makes it impossible to add it to the integer (thus the " TypeError: unsupported operand type(s) " error). 这里的问题是你将pyodbc.Row实例(由.fetchone()返回.fetchone()分配给starting_index ,这使得无法将它添加到整数(因此“ TypeError:unsupported operand type(s) ”错误)。

Solution

Try to replace this line: 尝试替换此行:

starting_index = cursor.fetchone()

with this line: 用这一行:

starting_index = cursor.fetchone()[0]

More reading 更多阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM