Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

storing result of MySQL query into python variable

Tags:

python

mysql

when I execute the following code using python programming language and MySQL database

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'
like image 397
vector8188 Avatar asked Jul 03 '26 06:07

vector8188


1 Answers

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).

Solution

Try to replace this line:

starting_index = cursor.fetchone()

with this line:

starting_index = cursor.fetchone()[0]

More reading

  • PEP 249 - Python Database API Specification v2.0 (especially part about fetchone())
like image 170
Tadeck Avatar answered Jul 04 '26 19:07

Tadeck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!