Python how to know if a record inserted successfully or not

28,302

You can use .rowcount attribute:

cursor.execute("""INSERT INTO `User`(`UID`, `IP`) VALUES(%s,%s);""", params)
print("affected rows = {}".format(cursor.rowcount))

.rowcount This read-only attribute specifies the number of rows that the last .execute*() produced (for DQL statements like SELECT) or affected (for DML statements like UPDATE or INSERT). [9]

Share:
28,302

Related videos on Youtube

Gia Duong Duc Minh
Author by

Gia Duong Duc Minh

Updated on October 01, 2020

Comments

  • Gia Duong Duc Minh
    Gia Duong Duc Minh over 3 years

    I'm using Python MySQL Connector, I inserted a record into database, and it was successful. But in Python code, how can I know if it is inserted or not? My Table does not have a primary key.

    def insert(params) :
        db_connection = Model.get_db_connection()
        cursor = db_connection.cursor()
        try :
            cursor.execute("""INSERT INTO `User`(`UID`, `IP`) VALUES(%s,%s);""", (params))
            db_connection.commit()
        except :
            db_connection.rollback()
        Model.close_db(db_connection)
        return result
    
  • Gia Duong Duc Minh
    Gia Duong Duc Minh over 10 years
    One more question @falsetru, I used .rowcount before posting this question. But it threw an error "'int' cannot callable". But now it worked, I really don't know why.
  • falsetru
    falsetru over 10 years
    @GiaDuongDucMinh, Did you call rowcount like method? .rowcount()? It is not a method.
  • Gia Duong Duc Minh
    Gia Duong Duc Minh over 10 years
    I cannot remember clearly, but maybe I made this mistakem @falsetru. Thanks!
  • Marc Maxmeister
    Marc Maxmeister over 8 years
    This is the ONLY way to know if INSERT IGNORE... inserted or not when inserting into a table whose primary key does not auto-increment. (I know because I just encountered this problem). checking cursor.lastrowid will always be zero in this case. (but when auto-increment is ON, lastrowid will not be zero)
  • code_dredd
    code_dredd almost 8 years
    Note that if you use an UPDATE statement and it just so happens that the data for the row update is already the same as what's already in the database, there will be a matching row, but no rows will be affected, keeping .rowcount at 0.
  • falsetru
    falsetru over 7 years
    @ShubhamChaudhary, What's the point of changing quotation mark?
  • Shubham Chaudhary
    Shubham Chaudhary over 7 years
    pep8 guidelines about strings
  • falsetru
    falsetru over 7 years
  • Shubham Chaudhary
    Shubham Chaudhary over 7 years
    @falsetru Ah, well sounds like it's only our company convention, my bad. Learned something new.
  • merlin
    merlin almost 4 years
    @falsetru I am wondering why this does not work at all: print("rows: " + cursor.rowcount)
  • falsetru
    falsetru almost 4 years
    @merlin, You're trying to concatenate int to str. print("rows: " + str(cursor.rowcount)), print("rows: {}".format(cursor.rowcount), ....
  • merlin
    merlin almost 4 years
    Thank you @falsetru That makes sense.
  • Umair Ayub
    Umair Ayub over 2 years
    for UPDATE, even rows data is changed, cursor.rowcount still returns 0
  • falsetru
    falsetru over 2 years
    @UmairAyub, See python.org/dev/peps/pep-0249/#id48. ... Most databases will return the total number of rows that were found by the corresponding WHERE clause of the statement. Some databases use a different interpretation for UPDATEs and only return the number of rows that were changed by the UPDATE, ....