Closed lbilodeau closed 2 years ago
OK, I figured out why the update wasn't working. Now I understand that one MUST commit the query either specifically or by setting the autocommit property on the connection.
one MUST commit the query either specifically or by setting the autocommit property on the connection.
True, but that's not why you were getting the error. You were getting the error because you were calling .fetchall()
when the cursor had no result set. You can check the cursor's .description
to determine that:
def show_cursor_status(crs):
descr = crs.description
if descr:
print("Previous SQL was a query.")
print(f"Results: {crs.fetchall()}")
else:
print("Previous SQL was not a query.")
print(f"Row count: {crs.rowcount}")
crsr.execute("create table #tmp (id int primary key)")
show_cursor_status(crsr)
"""
Previous SQL was not a query.
Row count: -1
"""
crsr.execute("insert into #tmp (id) values (1), (2)")
show_cursor_status(crsr)
"""
Previous SQL was not a query.
Row count: 2
"""
crsr.execute("select * from #tmp")
show_cursor_status(crsr)
"""
Previous SQL was a query.
Results: [(1, ), (2, )]
"""
crsr.execute("delete from #tmp where id = 2")
show_cursor_status(crsr)
"""
Previous SQL was not a query.
Row count: 1
"""
Environment
HOST: Ubuntu 20.04.4 LTS (GNU/Linux 5.13.0-1031-azure x86_64)
Issue
When attempting to execute a simple update command no changes are made to my data in the database and it seems that the pyodbc is ignoring the query.
Here's what I have set up:
and this is the query and the exception I get:
the query works fine from SSMS.
I don't understand why pyodbc is not processing my UPDATE query. Does anyone else have an idea why this query isn't processed properly.