Search code examples
pythonsqlsql-updatepymssql

pymssql sql update statement


I have the below syntax. The first cursor.execute \ fetchone pulls back the one row that I want to update. But when I try to run the update query, it updates multiple records.

cursor.execute("Select * FROM [DisneyConvoIndex] where [ConversationBaseIndex]=%s order by length DESC;", '0101CFE27C560BEB6C1073FBF741997D79EDBD4610AC')

row = cursor.fetchone()

for row in row:
    cursor.execute("UPDATE [DisneyConvoIndex] set [toreview] = 'yes'")

Solution

  • You're trying to update the row after it's returned. You're just dealing with the data at that point, not the object in the DB. You need to limit the rows you're updating with a where clause. So, use something like

    cursor.execute("UPDATE [DisneyConvoIndex] set [toreview] = 'yes' WHERE [ConversationBaseIndex]=%s", '0101CFE27C560BEB6C1073FBF741997D79EDBD4610AC')