The error message “pyodbc.programmingerror: no results. previous sql was not a query.”
indicates that the previous SQL statement executed using the pyodbc library did not return any results.
This error typically occurs when you try to fetch results from a SQL statement that is not a query, such as an INSERT, UPDATE, or DELETE statement.
These types of SQL statements do not return results, so trying to fetch them will result in this error.
To demonstrate this with an example, let’s say we have the following code using pyodbc library to execute an INSERT statement:
import pyodbc
# Connect to the database
connection = pyodbc.connect('DSN=your_dsn;UID=username;PWD=password')
# Create a cursor
cursor = connection.cursor()
# Execute an INSERT statement
cursor.execute("INSERT INTO your_table (column1, column2) VALUES (value1, value2)")
# Try to fetch results, which will result in the error
row = cursor.fetchone()
In the above example, we are trying to fetch results using the fetchone()
method after executing an INSERT statement.
Since INSERT statement does not return any results, it will raise the “pyodbc.programmingerror: no results” error.
To avoid this error, you should only try to fetch results from SQL statements that actually return rows, such as SELECT statements.
For non-query statements like INSERT, UPDATE, or DELETE, there is no need to fetch any results.