0

I am writing a simple python program to retrieve information from a localhost database i set up.

I am encountering the following error:

('24000', '[24000] [Microsoft][ODBC Driver 17 for SQL Server]Invalid cursor state')

My code:

       try: 
            cursor = conn.cursor()
            cursor.execute(getSQLStatement('SelectRecipe'),[item])
            recipe = cursor.fetchone()[0]
            cursor.close()
        except Exception as e:
            print(e)
            return

The query i am running works fine in the database client, the query isnt the issue, it is a simple SELECT statement:

SELECT 
     Recipe
FROM recipes
WHERE Name = ? --(item) 

My desired result is just the most basic statement just so i can get it running, and then be able to expand the complexity later, but something is wrong at this level. I am just trying to get the value of one record and one column i believe with this code. When i searched other similar issues people had with this the answer was always related to multiple result sets being attempted to be returned? My query is just one result set and i am only running it once? I am kind of scratching my head here to find what i am missing?

The error seems to be related to the:

recipe = cursor.fetchone()[0]

...line as it runs fine without throwing the error without this line, however then i am not sure how to get the data as a variable if i dont do it that way.

Wondering if anyone could help me out here, if there is a better way to get my data or if there is something wrong with my implementation of this way. Thanks.

Geedubs123
  • 69
  • 8
  • you may want to print the results of your cursor.fetchone() statement, before the line recipe = cursor.fetchone()[0], and make sure you have the expected data in that line of your program. – Rob Py Jan 26 '21 at 11:30
  • it doesnt get that far, that statement is what is throwing the error, the expected data isnt even coming in(?) for some reason. i believe the cursor has it after the .execute statement but when i am trying to piece it together(?) is where the error is occuring? – Geedubs123 Jan 26 '21 at 11:56
  • That is not true, it is part of storing the query in python and it works just fine inserting, i ran into this issue when selecting. the statement is fine i just included a basic version for context – Geedubs123 Jan 26 '21 at 13:40

0 Answers0