I am connecting to a MySQL database through a Python environment. I'm using pypyodbc as the connector. Everything works fine, I can Select and Insert results, etc.
The issue comes in when I attempt to actually assign results fetched from the database to a variable: It literally only works sometimes, and rarely.
Here's some code:
SQLCommand = ("SELECT TRIM(' ' FROM ColumnName) FROM MyDB.dbo.MyTable WHERE ColumnName='%s'" % (my_variable)
cursor.execute(SQLCommand)
print("Here is the result from the database ", cursor.fetchone())
another_variable = cursor.fetchone()
print("Here is the value from the database assigned to a variable: ", another_variable)
As you can see I attempt to assign the fetched result to a variable at:
another_variable = cursor.fetchone()
The word I'm fetching will be in the database plain as day. Cursor.fetchone will fetch the result and print it every time. But it will only assign it to a variable sometimes. Rarely.
I'm baffled, is there some known issue doing this? Am I somehow missing anything? I need it to assign the result to the cursor everytime, not rarely.
cursor.fetchone()a second time it will attempt to fetch a second row. Do you have multiple rows that satisfy the condition?