title: Inserting with pyodbc in SQL Server table that has two columns as primary key
body: <p>I am using pyodbc to insert a custom pricelist in SQL Server. My other queries work on the server, but I have difficulty with the following.  When I use SQL Server Management Studio the values are nicely inserted:</p>
  <pre><code>INSERT INTO abacus_raster.dbo.artikal_cijenovnik_cijena (id_cijenovnik, id_artikal, cijena, order_id, cijena_nab) 
  VALUES (13, 46642, 3.30, 1, 2.82)
  </code></pre>
  <p>This code does not work in pyodbc:</p>
  <pre><code>import pyodbc
  cnxn = pyodbc.connect('Driver={SQL Server};'
                        'Server=SERVER;'
                        'Database=abacus_raster;'
                        'Trusted_Connection=yes;')
  cursor = cnxn.cursor()    
  cursor.execute(&quot;&quot;&quot;INSERT INTO abacus_raster.dbo.artikal_cijenovnik_cijena 
          (id_cijenovnik, id_artikal, cijena, order_id, cijena_nab) VALUES(13, 46642, 3.30, 1, 2.82)&quot;&quot;&quot;)
  </code></pre>
  <p>I get this error:</p>
  <blockquote>
  <p>Traceback (most recent call last):</p>
  <p>File &quot;C:/dev/cijene/cijene.py&quot;, line 109, in <br />
  (id_cijenovnik, id_artikal, cijena, order_id, cijena_nab) VALUES(13, 46642, 3.30, 1, 2.82)&quot;&quot;&quot;)</p>
  <p>pyodbc.IntegrityError: ('23000', u&quot;[23000] [Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY KEY constraint 'PK_artikal_cijenovnik_cijena'. Cannot insert duplicate key in object 'dbo.artikal_cijenovnik_cijena'. (2627) (SQLExecDirectW); [23000] [Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been terminated. (3621)&quot;)</p>
  </blockquote>
  <p>ODBC trace from SSMS:</p>
  <blockquote>
  <p>cijene          155c-105c ENTER SQLExecDirect
  HSTMT               00D91BA8
  UCHAR *             0x00AEABB4 [     156] &quot;INSERT INTO abacus_raster.dbo.artikal_cijenovnik_cijena \ a        (id_cijenovnik, id_artikal, cijena, order_id, cijena_nab) VALUES(13, 46642, 2.883, 1, 2.62)&quot;
  SDWORD                   156</p>
  <p>cijene          155c-105c EXIT  SQLExecDirect  with return code 0 (SQL_SUCCESS)
  HSTMT               00D91BA8
  UCHAR *             0x00AEABB4 [     156] &quot;INSERT INTO abacus_raster.dbo.artikal_cijenovnik_cijena \ a        (id_cijenovnik, id_artikal, cijena, order_id, cijena_nab) VALUES(13, 46642, 2.883, 1, 2.62)&quot;
  SDWORD                   156</p>
  </blockquote>
  <p>ODBC Trace from python:</p>
  <blockquote>
  <p>cijene          155c-105c ENTER SQLExecDirect
  HSTMT               00D91BA8
  UCHAR *             0x00AEABB4 [     156] &quot;INSERT INTO abacus_raster.dbo.artikal_cijenovnik_cijena \ a        (id_cijenovnik, id_artikal, cijena, order_id, cijena_nab) VALUES(13, 46642, 2.883, 1, 2.62)&quot;
  SDWORD                   156</p>
  <p>cijene          155c-105c EXIT  SQLExecDirect  with return code -1 (SQL_ERROR)
  HSTMT               00D91BA8
  UCHAR *             0x00AEABB4 [     156] &quot;INSERT INTO abacus_raster.dbo.artikal_cijenovnik_cijena \ a        (id_cijenovnik, id_artikal, cijena, order_id, cijena_nab) VALUES(13, 46642, 2.883, 1, 2.62)&quot;
  SDWORD                   156</p>
  <p>DIAG [23000] [Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY KEY constraint 'PK_artikal_cijenovnik_cijena'. Cannot insert duplicate key in object 'dbo.artikal_cijenovnik_cijena'. (2627)</p>
  <p>DIAG [01000] [Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been terminated. (3621)</p>
  </blockquote>
  <p>This is how the table looks:</p>
  <p><a href="https://i.stack.imgur.com/9ZYa8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9ZYa8.png" alt="Table definition" /></a></p>