I am relatively new to pyodbc and have come across this issue where no error is thrown for certain SQL violations.
In a single SQL statement I carry out the following steps:
- create a table with primary key
- Insert a value
foo
- Insert the same value from step 2 (to break the statement)
The statement executes in pyodbc until Step 2 as I can see the value '2' in the database. Step 3 should throw a primary key exception (ie an integrity error in pyodbc) however nothing is given. No indication is given by pyodbc to show an error has occured and it seems the statement works just fine.
Here is my code:
import pyodbc
conn = pyodbc.connect(conn_str, autocommit=False)
statement = """
CREATE TABLE test_table_name (TEST_INT_COL INT, PRIMARY KEY (TEST_INT_COL));
INSERT INTO test_table_name (TEST_INT_COL) VALUES (2);
INSERT INTO test_table_name (TEST_INT_COL) VALUES (2); #violates primary key constraint here
"""
cursor = conn.cursor()
cursor.execute(statement)
conn.commit()
I'm wondering if there is something I have done wrong? or is there something wrong with pyodbc that requires further inspection?
Thanks in advance